Encapsulation in Python
Encapsulation is one of the four pillars of OOP.
Definition
Encapsulation means combining data and the methods that operate on that data inside a class, while controlling how the data can be accessed or changed.
In simple words:
Encapsulation = Data + Methods + Controlled Access
1. Why Encapsulation?
Encapsulation is mainly used to:
- Protect data from unwanted changes
- Control how data is accessed
- Keep related data and methods together
- Make programs more organized and maintainable
2. Access Levels in Python
Python commonly uses three naming conventions:
| Type | Syntax | Meaning |
|---|---|---|
| Public | name | Can be accessed normally |
| Protected | _name | Intended for internal/subclass use |
| Private | __name | Intended to restrict direct access |
3. Public Members
A public member can be accessed directly.
class Student: def __init__(self, name): self.name = name
Here:
self.name
is public.
It can be accessed directly:
student = Student("Ram") print(student.name)
Output:
Ram
4. Protected Members
A protected member starts with a single underscore _.
class Student: def __init__(self, name): self._name = name
The underscore indicates:
This member is intended for internal use or use by subclasses.
Example:
student = Student("Ram") print(student._name)
Python still allows this access.
So _name is not truly private. It is mainly a convention.
5. Private Members
A private member starts with two underscores __.
class BankAccount: def __init__(self, balance): self.__balance = balance
Now direct access like:
account.__balance
does not work normally.
Python uses name mangling for double-underscore attributes.
6. Controlling Private Data
Instead of allowing direct access, we can provide methods to control the data.
class BankAccount: def __init__(self, balance): self.__balance = balance def get_balance(self): return self.__balance def deposit(self, amount): if amount > 0: self.__balance += amount
Here:
__balance→ private dataget_balance()→ controlled accessdeposit()→ controlled modification
This is the main idea of encapsulation.
7. Getter
A getter is a method used to retrieve private data.
def get_balance(self): return self.__balance
It allows us to read the value without directly accessing the private variable.
8. Setter
A setter is a method used to modify private data.
def set_age(self, age): if age >= 0: self.__age = age
The setter can validate the value before changing it.
For example:
class Student: def __init__(self, age): self.__age = age def get_age(self): return self.__age def set_age(self, age): if age >= 0: self.__age = age
Here, an invalid negative age can be rejected.
9. Encapsulation with @property
Python also provides @property for controlled access.
class Student: def __init__(self, age): self.__age = age @property def age(self): return self.__age @age.setter def age(self, value): if value >= 0: self.__age = value
Now we can use:
student.age = 21 print(student.age)
instead of calling getter and setter methods directly.
10. Encapsulation vs Data Hiding
These terms are related but not exactly the same.
Encapsulation
Combining:
Data + Methods
inside a class and controlling access.
Data Hiding
Restricting direct access to internal data.
For example:
self.__balance
is used to hide the internal balance from normal direct access.
So:
Data hiding is one part of encapsulation.
11. Main Points to Remember
Encapsulation | +-- Combine data and methods | +-- Control access to data | +-- Public members | +-- Protected members (_name) | +-- Private members (__name) | +-- Getter | +-- Setter | +-- @property
Simple definition for exams
Encapsulation is an OOP concept that combines data and methods into a single class and controls access to the internal data.