Trusted answers to developer questions

What is encapsulation in Python?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The process of wrapping up variables and methods into a single entity is known as Encapsulation. It is one of the underlying concepts in object-oriented programming (OOP). It acts as a protective shield that puts restrictions on accessing variables and methods directly, and can prevent accidental or unauthorized modification of data. Encapsulation also makes objects into more self-sufficient, independently functioning pieces.

Encapsulation
Encapsulation

Access modifiers

Access modifiers limit access to the variables and functions of a class. Python uses three types of access modifiers; they are - private, public and protected.

Public members

Public members are accessible anywhere from the class. All the member variables of the class are by default public.

# program to illustrate public access modifier in a class
class edpresso:
# constructor
def __init__(self, name, project):
self.name = name;
self.project = project;
def displayProject(self):
# accessing public data member
print("Project: ", self.project)
# creating object of the class
edp = edpresso("TeamPhoenix", 1);
# accessing public data member
print("Name: ", edp.name)
# calling public member function of the class
edp.displayProject()

In class edpresso, the variable name and project are public. These data members can be accessed anywhere in the program.

Protected members

Protected members are accessible within the class and also available to its sub-classes. To define a protected member, prefix the member name with a single underscore “_”.

# program to illustrate protected access modifier in a class
class edpresso:
def __init__(self, name, project):
self._name = name
self._project = project
# creating object of the class
edp = edpresso("TeamPhoenix", 2)
# direct access of protected member
print("Name:",edp._name)
print("project:",edp._project)

The variable name and project of class edpresso are protected; hence, it is accessed as _name, and _project respectively.

Private members

Private members are accessible within the class. To define a private member, prefix the member name with a double underscore “__”.

# program to illustrate private access modifier in a class
class edpresso:
def __init__(self, name, project):
# public variable
self.name = name
# private variable
self.__project = project
# creating an instance of the class Sample
edp = edpresso('TeamPhoenix', 3)
# accessing public variable name
print("Name:",edp.name)
# accessing private variable __project using
# _Edpresso__project name
print("Project:",edp._edpresso__project)

In class edpresso, __project is a private variable; hence, it is accessed by creating an instance.

Merits of encapsulation

svg viewer
  • Aesthetics: Encapsulation makes the application appealing and more comfortable to understand.
  • Authority: Encapsulation protects an object from unauthorized access. It allows access to a level without revealing the intricate details below that level.
  • Simplicity: Simplifies the maintenance of the application, reduces human errors.

Need for encapsulation in Python

  • Encapsulation helps to achieve well-defined, readable code.
  • Users can securely maintain the applications.
  • Encapsulation ensures the code’s simplicity and flexibility through proper code organization and assists with a smooth user experience.

Thanks for reading!

Happy Coding.

RELATED TAGS

python
encapsulation
oop
edpresso competition
Did you find this helpful?