Encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that helps protect the internal state of an object by restricting access to its attributes and methods. It allows you to control how data is accessed and modified.
What is Encapsulation?
Encapsulation is the process of wrapping data and methods into a single unit (class). In Python, this is achieved using access modifiers that control the visibility of variables and methods.
Types of Access Modifiers in Python
- Public: Accessible from anywhere.
- Protected: Accessible within the class and its subclasses (denoted by a single underscore
_variable
). - Private: Accessible only within the class (denoted by a double underscore
__variable
).
1. Public Members
Public members can be accessed from anywhere in the code.
Example:
class Person: def __init__(self, name, age): self.name = name # Public attribute self.age = age # Public attribute person = Person("Alice", 30) print(person.name) # Output: Alice print(person.age) # Output: 30
2. Protected Members
Protected members are prefixed with a single underscore _
. They can be accessed within the class and its subclasses.
Example:
class Person: def __init__(self, name, age): self._name = name # Protected attribute self._age = age # Protected attribute class Employee(Person): def get_details(self): return f"Name: {self._name}, Age: {self._age}" employee = Employee("Bob", 25) print(employee.get_details()) # Output: Name: Bob, Age: 25
3. Private Members
Private members are prefixed with a double underscore __
. They are accessible only within the class.
Example:
class Person: def __init__(self, name, age): self.__name = name # Private attribute self.__age = age # Private attribute def get_info(self): return f"Name: {self.__name}, Age: {self.__age}" person = Person("Charlie", 40) print(person.get_info()) # Output: Name: Charlie, Age: 40 # Accessing private attribute directly will raise an AttributeError # print(person.__name) # Uncommenting this line will raise an error
Accessing Private Members
Private members can be accessed using name mangling, where the attribute name is prefixed with _ClassName
.
Example:
class Person: def __init__(self, name): self.__name = name # Private attribute person = Person("Diana") print(person._Person__name) # Output: Diana
Getter and Setter Methods
Encapsulation is often used with getter and setter methods to provide controlled access to private attributes.
Example:
class Person: def __init__(self, name): self.__name = name # Private attribute # Getter method def get_name(self): return self.__name # Setter method def set_name(self, new_name): self.__name = new_name person = Person("Eve") print(person.get_name()) # Output: Eve person.set_name("Sophia") print(person.get_name()) # Output: Sophia
Conclusion
Encapsulation helps protect an object’s internal state by restricting direct access to its attributes. By using public, protected, and private members along with getter and setter methods, you can create more secure and maintainable Python code.