
10 Types of Programming Languages Every Coder should Know
Learn about different types of programming languages and how they are implemented in the real world.

The Object-Oriented Programming language has four principles. In this article, we will discuss the Encapsulation principle, a simple yet powerful concept in Python. It involves bundling data members and functions inside a single class, making your code more organised and easier to manage. The benefits of this are immense, and we’ll explore them in detail. Let’s dive deep into Encapsulation in the Python language for more information.
Encapsulation in Python refers to the bundling of data and methods into a single unit, which is called a class. The idea behind encapsulation is to hide an object’s internal state and only expose the necessary functionalities. This can be achieved by marking certain attributes or methods. Encapsulation in Python also ensures that objects are self-sufficient functioning pieces and work independently.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Let’s see why we need an Encapsulation in Python language:
In Python language, sometimes we have to restrict or limit access to certain variables of function while programming. That is where access modifiers come into the picture in Python. There are three types of access modifiers:
| Access Specifier | Access From own class | Accessible from derived class | Accessible from object |
| Private Member | Yes | No | No |
| Protected Member | Yes | Yes | No |
| Public Member | Yes | Yes | Yes |
In this section, we will use the public modifier that allows variables and functions to be accessible from anywhere within the class and from any part of the program. All the members’ variables have the access modifier as public or default.
The following program demonstrates the Encapsulation in Python using the public modifiers:
Program
class Modifier:
# constructor
def __init__(self, name, age):
self.name = name;
self.age = age;
def Age(self):
# Accessing public data member
print("Age: ", self.age)
obj = Modifier(“Murli", 50);
print("Name: ", obj.name)
obj.Age()
Output
Name: Murli
Age: 50
Also Read: Polymorphism in Python

82.9%
of professionals don't believe their degree can help them get ahead at work.
In this section, we will use a private access modifier that allows member methods and variables to be accessed only within the class. To specify a private access modifier for a member, we make use of the double underscore__
The following program demonstrates the Encapsulation in Python using private members:
Program
class Rectangle:
__length = 0
__breadth = 0
def __init__(self):
#constructor
self.__length = 33
self.__breadth = 343
print(self.__length)
print(self.__breadth)
rect = Rectangle()
print(rect.length)
print(rect.breadth)
Output
33
343
ERROR!
Traceback (most recent call last):
File "<main.py>", line 12, in <module>
AttributeError: 'Rectangle' object has no attribute 'length'
In the above code, we have received an AttributeError in the output. In the above example, we tried to access it outside the class, which is why we received the above error. We can access private members from the outside of a class using the following two approaches:
The following program demonstrates the Public method of accessing private members:
Program
# Public method access to private members
class details:
_name="Rahul"
_age=2343
_job="Content Writer"
class pro_mod(details):
def __init__(self):
print(self._name)
print(self._age)
print(self._job)
# Here, we are creating an object
obj = pro_mod()
Output
Murli
2343
Content Writer
In this section, we can directly access private and protected variables from the outside of a class. The name mangling is created on an identifier by adding two leading underscores and one trailing underscore, like this _classname_dataMember, where the class name is the current class, and the data is the private variable name.
The following program demonstrates Name Mangling to access private members:
Program
class details:
_name="Bullet"
_age=3343
_job="Biker"
class pro_mod(details):
def __init__(self):
print(self._name)
print(self._age)
print(self._job)
# creating object of the class
obj = pro_mod()
# direct access to protected member
print("Name:",obj._name)
print("Age:",obj._age)
Output
Neha
3343
Don't know
Name: Neha
Age: 3343
In this section, we will use the protected member in the Python language. That allows the members to access the class and allows them to be accessed by the sub-class involved. In Python language, we demonstrated a protected member. we know that if the members have a protected access specifier, it can be referenced within the class and the subsequent sub-class. The following program demonstrates the Encapsulation in Python using the protected members:
Program
# illustrating protected members & protected access modifier
class details:
_name="Jason"
_age=35
_job="Developer"
class pro_mod(details):
def __init__(self):
print(self._name)
print(self._age)
print(self._job)
# creating object of the class
obj = pro_mod()
# direct access to protected member
print("Name:",obj.name)
print("Age:",obj.age)
Output
Jason
35
Developer
ERROR!
Traceback (most recent call last):
File "<main.py>", line 15, in <module>
AttributeError: 'pro_mod' object has no attribute 'name'
Encapsulation in Python offers numerous advantages. There are also some potential disadvantages of Encapsulation:
In this article, we have explored encapsulation in the Python language. Encapsulation is a powerful concept that allows developers to control access to certain parts of an object, thereby protecting its internal state and preventing unintended modification of the code. By using encapsulation, we can hide the implementation details of a class and only expose the necessary components of the code. It promotes better code readability, maintenance and security.
Updated on July 18, 2024

Learn about different types of programming languages and how they are implemented in the real world.

Explore 10 front-end development, including key languages, its advantages and disadvantages, and how it shapes user experience in web design and functionality.