Encapsulation in Python
Veeg om het menu te tonen
Encapsulation is a core principle of object-oriented programming (OOP) that refers to the bundling of data (attributes) and methods (functions) that operate on that data within a single unit, typically a class. The main goal of encapsulation is to restrict direct access to some of an object's components, which helps to prevent accidental modification of data and to maintain the integrity of the object's state. By controlling how data is accessed and modified, you can make your code more robust, easier to maintain, and less prone to bugs.
1234567891011121314151617181920212223class BankAccount: def __init__(self, owner, balance): self.owner = owner # Public attribute self._balance = balance # "Protected" attribute (by convention) self.__account_number = 123456789 # "Private" attribute (name mangling) def deposit(self, amount): self._balance += amount def withdraw(self, amount): if amount <= self._balance: self._balance -= amount else: print("Insufficient funds.") def get_account_number(self): return self.__account_number account = BankAccount("Alice", 1000) print(account.owner) # Accessible: prints "Alice" print(account._balance) # Accessible, but discouraged by convention # print(account.__account_number) # AttributeError: not directly accessible print(account.get_account_number()) # Accessible via method
In Python, encapsulation relies on naming conventions rather than strict access control. By default, all attributes and methods are public, meaning they can be accessed from outside the class. However, you can indicate intended privacy levels using underscores:
- A single leading underscore (such as
_balance) signals that an attribute or method is intended for internal use only. This is a convention to discourage external access, but it does not prevent it; - A double leading underscore (such as
__account_number) triggers name mangling, where Python internally renames the attribute to include the class name. This makes it harder (but not impossible) to access from outside the class, providing a stronger suggestion of privacy.
In the BankAccount example, owner is public, _balance is "protected" by convention, and __account_number is "private" due to name mangling. To allow controlled access to private data, you can provide public methods like get_account_number().
Encapsulation is a core principle of object-oriented programming (OOP) that refers to the bundling of data (attributes) and methods (functions) that operate on that data within a single unit, typically a class. The main goal of encapsulation is to restrict direct access to some of an object's components, which helps to prevent accidental modification of data and to maintain the integrity of the object's state. By controlling how data is accessed and modified, you can make your code more robust, easier to maintain, and less prone to bugs.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.