Encapsulation in Python
Encapsulation is a fundamental concept in object-oriented programming (OOP). It defines the concept of data wrapping and the methods that work on data within a single unit. This restricts direct access to variables and methods and can prevent data from being accidentally modified. To prevent accidental changes, an object’s variables can only be changed by an object’s method to prevent accidental changes. These variables are referred to as “private variables.”
A class is an example of encapsulation because it contains all of the data that is contained in member functions, variables, and so on.
Consider a real-world example of encapsulation: in a company, there are various sections, such as the accounts section, the finance section, the sales section, and so on. The finance section handles all financial transactions and keeps track of all financial data. Similarly, the sales section is in charge of all sales-related activities and keeps track of all sales. For some reason, an official from the finance department may require all sales data for a specific month. In this case, he is not permitted to directly access the sales section’s data. He must first contact another officer in the sales section and request specific data from him. Encapsulation is what it is.
Here, the sales section’s data and the employees who can manipulate it are bundled under the umbrella term “sales section.” Encapsulation also conceals the data. In this example, data from sections such as sales, finance, and accounts are hidden from all other sections.
Protected members (in C++ and JAVA) are class members that can only be accessed from within the class and its subclasses and cannot be accessed from outside of the class. In Python, simply follow the convention by prefixing the member’s name with a single underscore “_”.
Although the protected variable can be accessed outside of the class as well as in the derived class (and modified in the derived class), it is customary (not a rule) not to access the protected variable outside of the class body.
class Base_Class: def __init__(self): self._number = 32 class Derived_Class(Base_Class): def __init__(self): Base_Class.__init__(self) print("Bringing up a protected member of a base class: ", self._number) self._number = 23 print("Outside of class, calling a modified protected member: ", self._number) First_Object = Derived_Class() Second_Object = Base_Class() print("Getting the First_Object's protected member: ", First_Object._number) print("Getting the Second_Object's protected member: ", Second_Object._number)
Bringing up a protected member of a base class: 32 Outside of class, calling a modified protected member: 23 Getting the First_Object's protected member: 23
Private members are similar to protected members; however, class members declared private should not be accessed outside of the class or by any base class. Private instance variables that can only be accessed within a class do not exist in Python.
To define a private member, however, prefix the member name with a double underscore ” __”.
class Base_Class: def __init__(self): self.val = "Python is awesome." self.__item = "Python is awesome." class Derived_Class(Base_Class): def __init__(self): Base_Class.__init__(self) print(self.__item) details = Base_Class() print(details.val)
Python is awesome.
Python Getters and Setters
Setters and getters are required in Python to implement proper encapsulation. The main reason getters and setters are used in object-oriented programs is to ensure data encapsulation. To access data members, use the getter method, and to modify data members, use the setter method.
Private variables in Python are not hidden fields like in other programming languages. The getters and setters methods are frequently employed when:
class Student: def __init__(self, name, age): self.name = name self.__age = age def get_age(self): return self.__age def set_age(self, age): self.__age = age details = Student('Rahim', 22) print('Name:', details.name, details.get_age()) details.set_age(26) print('Name:', details.name, details.get_age())
Name: Rahim 22 Name: Rahim 26
Advantages of Encapsulation
Security: The primary benefit of encapsulation is data security. Encapsulation safeguards an object against unauthorized access. It provides private and protected access levels to prevent data modification by accident.
Data concealment: The user would be unaware of what is going on behind the scenes. They would only know that to change a data member, they should use the setter method. Call the getter method to read a data member. They have no idea what these setter-and-getter methods are doing.
Simplicity: It simplifies application maintenance by keeping classes separated and preventing them from tight coupling with one another.
Aesthetics: Combining data and methods into a single class makes code more readable and maintainable.