Encapsulation
Encapsulation means protecting an object's data: you don't change it directly, you go through methods that check that everything stays consistent.
By convention, an attribute that starts with an underscore, like self._balance, is internal: you shouldn't touch it from outside. The @property decorator lets you read a value like an attribute (acc.balance) while keeping control.
class BankAccount:
def __init__(self, owner):
self.owner = owner
self._balance = 0
def deposit(self, amount):
if amount > 0:
self._balance += amount
@property
def balance(self):
return self._balance
acc = BankAccount("Sara")
acc.deposit(50)
acc.deposit(-10)
print(acc.balance)