Introduction ๐
**Encapsulation** is one of the core principles of Object-Oriented Programming (OOP). It refers to **binding data and methods together** and **controlling access** to that data. Encapsulation helps protect internal object data from accidental modification.
Note
Python achieves this using private, protected attributes and getter/setter methods.
1. Why Encapsulation? ๐ค
- โ Prevents direct modification of internal data
- โ Adds security and control
- โ Allows validation before updating values
- โ Organizes code for maintainability
2. Protected Attributes (_single underscore) ๐ก๏ธ
A single underscore (_attribute) indicates that the attribute is **protected** and should not be accessed directly outside the class.
protected_attribute.py
class Person:
def __init__(self, name):
self._name = name # protected attribute
p = Person("Sathish")
print(p._name) # technically allowed but discouragedNote
3. Private Attributes (__double underscore) ๐
Double underscore (__attribute) makes the attribute **private**, meaning it is not accessible directly from outside the class.
private_attribute.py
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private
acc = BankAccount(1000)
print(acc.__balance) # โ AttributeErrorโ Python performs name mangling:__balance becomes _BankAccount__balance internally.
4. Accessing Private Attributes Using Getters & Setters ๐ง
getter_setter_encapsulation.py
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def get_balance(self): # getter
return self.__balance
def set_balance(self, amount): # setter
if amount < 0:
raise ValueError("Invalid amount")
self.__balance = amount
acc = BankAccount(500)
print(acc.get_balance())
acc.set_balance(800)
print(acc.get_balance())โ Allows data validation and safe access
5. Encapsulation Using @property Decorators ๐
property_encapsulation.py
class Employee:
def __init__(self, salary):
self._salary = salary
@property
def salary(self):
return self._salary
@salary.setter
def salary(self, value):
if value < 0:
raise ValueError("Salary cannot be negative")
self._salary = value
e = Employee(30000)
e.salary = 35000
print(e.salary)Note
6. Encapsulation Example โ Student Marks System ๐งฎ
student_marks.py
class Student:
def __init__(self, marks):
self.__marks = marks
@property
def marks(self):
return self.__marks
@marks.setter
def marks(self, value):
if not (0 <= value <= 100):
raise ValueError("Marks must be between 0 and 100")
self.__marks = value
s = Student(85)
s.marks = 95
print(s.marks)โ Fully encapsulated with validation.
7. Encapsulation Example โ Online Shopping Cart ๐
cart_example.py
class Cart:
def __init__(self):
self.__items = [] # private list
@property
def items(self):
return self.__items
def add_item(self, product):
self.__items.append(product)
c = Cart()
c.add_item("Laptop")
c.add_item("Mouse")
print(c.items)โ Items list cannot be modified directly
โ All changes go through controlled methods
8. Encapsulation with Private Methods ๐
private_method.py
class Demo:
def __private_method(self):
print("This is private")
def public_method(self):
self.__private_method()
d = Demo()
d.public_method()โ Private methods are used internally to restrict logic.
9. Name Mangling in Python ๐งฉ
name_mangling.py
class Test:
def __init__(self):
self.__value = 10
t = Test()
print(t._Test__value) # Accessing private attribute using name manglingNote
10. Encapsulation vs Abstraction โ๏ธ
| Encapsulation | Abstraction |
|---|---|
| Protects data using private/protected members | Hides complexity from the user |
| Focuses on โHow to protect?โ | Focuses on โWhat to show?โ |
| Achieved using getters, setters, @property | Achieved using abstract classes/methods |
11. Best Practices ๐ก
- โ Use _attribute for protected data
- โ Use __attribute for private data
- โ Prefer @property for encapsulation
- โ Avoid unnecessary private attributes
- โ Use methods to safely modify sensitive data
Conclusion ๐
You now fully understand Encapsulation in Python! Want the next topic? Try Inheritance, Polymorphism, Magic Methods, or OOP Exercises. Just tell me! ๐