Encapsulation in Python is a fundamental principle of object-oriented programming (OOP) that focuses on wrapping data (variables) and the methods that operate on the data into a single unit, typically a class. This mechanism restricts direct access to some of an object's components, which is a method of preventing unintended interference and misuse.
Encapsulation in Python enhances code organization and integrity by preventing external interference and unintended modification of class data. This results in:
Encapsulation in Python is implemented using the following components:
Accessible from anywhere inside or outside the class.
class Person: def __init__(self, name): self.name = name # Public variable person = Person("Alice") print(person.name) # Accessible
Denoted with a single underscore prefix (_variable). These members are intended for internal use within the class or its subclasses.
class Person: def __init__(self, name, age): self.name = name self._age = age # Protected variable person = Person("Bob", 30) print(person._age) # Accessible but not recommended
Private members use a double underscore prefix (__variable), making them inaccessible directly from outside the class.
class Person: def __init__(self, name, salary): self.name = name self.__salary = salary # Private variable person = Person("Carol", 70000) # print(person.__salary) # Raises AttributeError
Python does not use explicit getter and setter methods like some other languages but supports them via property decorators.
class Employee: def __init__(self, name): self.__name = name def get_name(self): return self.__name def set_name(self, name): if name != "": self.__name = name emp = Employee("David") print(emp.get_name()) # Output: David emp.set_name("Daniel") print(emp.get_name()) # Output: Daniel
class Employee: def __init__(self, name): self.__name = name @property def name(self): return self.__name @name.setter def name(self, value): if value: self.__name = value emp = Employee("Eva") print(emp.name) # Access using property emp.name = "Ella" # Modify using setter print(emp.name)
Consider a banking system where balance should not be modified directly:
class BankAccount: def __init__(self, owner, balance): self.owner = owner self.__balance = balance def deposit(self, amount): if amount > 0: self.__balance += amount def withdraw(self, amount): if 0 < amount <= self.__balance: self.__balance -= amount def get_balance(self): return self.__balance account = BankAccount("Frank", 1000) account.deposit(500) account.withdraw(200) print(account.get_balance()) # Output: 1300
Encapsulation in Python is a powerful programming concept that provides control over how data is accessed and modified. It improves code readability, maintainability, and security by hiding implementation details and exposing only what is necessary. With the use of access modifiers and property decorators, Python developers can implement encapsulation effectively in both simple and complex applications.
Copyrights © 2024 letsupdateskills All rights reserved