Python Classes

Python Encapsulation

Data encapsulation

Python Encapsulation

Encapsulation is the bundling of data and methods that work on that data within a single unit (class).

Private Attributes

In Python, we use underscore prefixes to indicate private attributes:

class BankAccount:
    def __init__(self, balance):
        self._balance = balance  # Protected
        self.__pin = "1234"      # Private
    
    def get_balance(self):
        return self._balance

account = BankAccount(1000)
print(account.get_balance())