Section 1/4Accessor (Getter) Method
Accessor (Getter) Method
A getter simply returns the private attribute. It does not modify anything.
Getters & Setters
When an attribute is private, external code cannot access it directly. Instead you provide accessor (getter) and mutator (setter) methods — the IB CS terminology. Setters can validate data before storing it.
Accessor (Getter) Method
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def get_balance(self): # accessor
return self.__balance
account = BankAccount(500)
print(account.get_balance()) # → 500→
500A getter simply returns the private attribute. It does not modify anything.