Lesson 28 of 55 · Python
OOP – Encapsulation
Duration: 20 minutes
Lesson 28 of 55 · Python
Duration: 20 minutes
python\nclass BankAccount:\n def __init__(self, balance=0):\n self.__balance = balance # private attribute\n\n @property\n def balance(self):\n return self.__balance\n\n def deposit(self, amount):\n if amount > 0:\n self.__balance += amount\n\naccount = BankAccount(100)\naccount.deposit(50)\nprint('Balance:', account.balance) # 150\n\n\nThe balance property is read‑only from the outside; only deposit can modify the underlying private attribute.\n