Encapsulation in python
Encapsulation in Python is the concept of bundling data (attributes) and methods (functions)
that operate on the data into a single unit, typically a class, while restricting direct access to
some of the object's components. This is mainly done to protect the internal state of an object
and to promote organized, secure, and readable code by hiding internal details and only
exposing what is necessary.
Core Understanding
Encapsulation means hiding the internal details of how a class works and preventing direct
modification of important data. It solves the problem of accidental data modification by allowing
controlled access through methods. Imagine a TV remote control: you interact with buttons
(methods) without needing to see or modify the internal wiring (data), which is hidden from you.
Syntax & Rules
In Python, encapsulation is done by convention using:
Public members: accessible directly (no underscore prefix).
Protected members: prefixed with a single underscore _ (should not be accessed
outside class/subclass).
Private members: prefixed with double underscores __ to make access harder from
outside the class (name mangling).
Private members cannot be accessed directly from outside and raise an AttributeError if
tried.
Controlled access is typically given via getter and setter methods.
Example:
class Employee:
def __init__(self, name, salary):
self.name = name # public attribute
self.__salary = salary # private attribute
def get_salary(self):
return self.__salary
def set_salary(self, salary):
if salary > 0:
self.__salary = salary
Why Encapsulation Exists
It ensures that the object’s data is protected from unauthorized or accidental modification,
supports abstraction by hiding complexity, and improves security and maintainability of code.
Examples
Simple example hiding salary attribute:
emp = Employee("Fedrick", 50000)
print(emp.name) # Works fine, public attribute
print(emp.__salary) # Raises AttributeError, private attribute
print(emp.get_salary()) # Access through public method works fine
Advantages
Protects object integrity by preventing outside code from directly modifying sensitive data.
Makes code easier to maintain and understand.
Enhances security by controlling access to internal data.
Facilitates modularity and reusability.
Summary
Encapsulation bundles data and methods inside a class and restricts direct access to some
components using name prefixes for access control. It is essential for safeguarding data
integrity and managing complexity in Python programs.
This explanation follows Python's approach to encapsulation using naming conventions and
shows why it is useful and how it works internally. [1] [2] [3] [4] [5]
⁂
1. https://www.geeksforgeeks.org/python/encapsulation-in-python/
2. https://www.tutorialspoint.com/python/python_encapsulation.htm
3. https://www.scaler.com/topics/python/encapsulation-in-python/
4. https://www.educative.io/answers/what-is-encapsulation-in-python
5. https://pynative.com/python-encapsulation/
6. https://www.wscubetech.com/resources/python/encapsulation
7. https://wiingy.com/learn/python/encapsulation-in-python/
8. https://www.upgrad.com/tutorials/software-engineering/python-tutorial/encapsulation-in-python/