Encapsulation in Python (original) (raw)
Last Updated : 27 Feb, 2025
In Python, encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, typically a class. It also restricts direct access to some components, which helps protect the integrity of the data and ensures proper usage.
Table of Content
Encapsulation is the process of hiding the internal state of an object and requiring all interactions to be performed through an object’s methods. This approach:
- Provides better control over data.
- Prevents accidental modification of data.
- Promotes modular programming.
Python achieves encapsulation through **public, **protected and **private attributes.
Encapsulation Python
How Encapsulation Works :
- **Data Hiding: The variables (attributes) are kept private or protected, meaning they are not accessible directly from outside the class. Instead, they can only be accessed or modified through the methods.
- **Access through Methods: Methods act as the interface through which external code interacts with the data stored in the variables. For instance, getters and setters are common methods used to retrieve and update the value of a private variable.
- **Control and Security: By encapsulating the variables and only allowing their manipulation via methods, the class can enforce rules on how the variables are accessed or modified, thus maintaining control and security over the data.
Example of Encapsulation
Encapsulation in Python is like having a bank account system where your account balance (data) is kept private. You can’t directly change your balance by accessing the account database. Instead, the bank provides you with methods (functions) like deposit and withdraw to modify your balance safely.
- **Private Data (Balance): Your balance is stored securely. Direct access from outside is not allowed, ensuring the data is protected from unauthorized changes.
- **Public Methods (Deposit and Withdraw): These are the only ways to modify your balance. They check if your requests (like withdrawing money) follow the rules (e.g., you have enough balance) before allowing changes.
Public Members
Public members are accessible from anywhere, both inside and outside the class. These are the default members in Python.
**Example:
Python `
class Public: def init(self): self.name = "John" # Public attribute
def display_name(self):
print(self.name) # Public method
obj = Public() obj.display_name() # Accessible print(obj.name) # Accessible
`
**Explanation:
- **Public Attribute (name): This attribute is declared without any underscore prefixes. It is accessible from anywhere, both inside and outside of the class.
- **Public Method (display_name): This method is also accessible from any part of the code. It directly accesses the public attribute and prints its value.
- **Object (obj): An instance of Public is created, and the display_name method is called, demonstrating how public attributes and methods can be accessed directly.
**Note: The __init__ method is a constructor and runs as soon as an object of a class is instantiated.
Protected members
Protected members are identified with a single underscore ****(_)**. They are meant to be accessed only within the class or its subclasses.
**Example:
Python `
class Protected: def init(self): self._age = 30 # Protected attribute
class Subclass(Protected): def display_age(self): print(self._age) # Accessible in subclass
obj = Subclass() obj.display_age()
`
**Explanation:
- **Protected Attribute (_age): This attribute is prefixed with a single underscore, which by convention, suggests that it should be treated as a protected member. It’s not enforced by Python but indicates that it should not be accessed outside of this class and its subclasses.
- **Subclass: Here, a subclass inherits from Protected. Within this subclass, we can still access the protected **attribute _age.
- **Method (display_age): This method within the subclass accesses the protected attribute and prints its value. This shows that protected members can be accessed within the class and its subclasses.
Private members
Private members are identified with a double underscore ****(__)** and cannot be accessed directly from outside the class. Python uses name mangling to make private members inaccessible by renaming them internally.
**Note: Python’s private and protected members can be accessed outside the class through python name mangling.
Python `
class Private: def init(self): self.__salary = 50000 # Private attribute
def salary(self):
return self.__salary # Access through public method
obj = Private() print(obj.salary()) # Works #print(obj.__salary) # Raises AttributeError
`
**Explanation:
- **Private Attribute (__salary): This attribute is prefixed with two underscores, which makes it a private member. Python enforces privacy by name mangling, which means it renames the attribute in a way that makes it hard to access from outside the class.
- **Method (salary): This public method provides the only way to access the private attribute from outside the class. It safely returns the value of __salary.
- **Direct Access Attempt: Trying to access the private attribute directly (obj.__salary) will result in an AttributeError, showing that direct access is blocked. This is Python’s way of enforcing encapsulation at a language level.