Encapsulation is one of the four fundamental concepts of object-oriented programming (OOP), along with abstraction, inheritance, and polymorphism. This lesson covers what encapsulation is and how to implement it in Python.
After reading this article, you will learn:
- Encapsulation in Python
- The need for encapsulation
- Data hiding using public, protected, and private members
- How name mangling works
- Data hiding vs. encapsulation
- Getter and setter methods, and the Pythonic
@propertydecorator - Benefits of encapsulation
Table of contents
What is Encapsulation in Python?
Encapsulation in Python describes the concept of bundling data and methods into a single unit. When you create a class, you are implementing encapsulation. A class is an example of encapsulation because it binds all the data members (instance variables) and the methods that work on them into a single unit.

Example:
In this example, we create an Employee class that stores employee attributes such as name and salary as instance variables and implements behavior using the work() and show() instance methods.
Output:
Name: Jessa Salary: 8000 Jessa is working on NLP
Using encapsulation, we can hide an object’s internal representation from the outside. This is called information hiding.
Encapsulation also lets us restrict direct access to variables and methods, and prevent accidental data modification, by making some data members and methods private within a class.
When a class deals with sensitive data, giving outside code access to every variable is not a good choice. Encapsulation lets us restrict access to methods and variables from outside the class.
For example, suppose an attribute is hidden from the outside of an object, and you provide methods that give read or write access to it. Then you control how the object’s internal state is read and changed. Encapsulation lets other code use the data it needs without giving it full access to every variable of the class. This protects an object’s data from other objects.
Access Modifiers in Python
Encapsulation can be achieved by declaring the data members and methods of a class as private or protected. Unlike Java or C++, Python has no public, private, or protected keywords. Instead, it uses naming conventions: a single underscore or double underscores at the start of a name.
Access modifiers limit access to the variables and methods of a class. Python has three levels:
- Public member (
name): Accessible anywhere, from inside or outside the class. - Private member (
__name): Accessible only within the class. - Protected member (
_name): Meant to be used only within the class and its subclasses.

Public Member
Public data members are accessible inside and outside of a class. All members of a class are public by default.
Example:
Output
Name: Jessa Salary: 10000 Name: Jessa Salary: 10000
Private Member
We can protect variables in a class by marking them private. To define a private variable, add two underscores as a prefix at the start of the variable name, for example __salary.
Private members are meant to be accessed only within the class. We can’t access them directly from an object using their original name.
Example:
Output
AttributeError: 'Employee' object has no attribute '__salary'
In the above example, __salary is a private variable, so we can’t access it from outside the class using that name.
We can access private members from outside a class in two ways:
- Create a public method that accesses the private members
- Use name mangling
Let’s see each one.
Public method to access private members
Example: Access a private member from outside the class using an instance method
Output:
Name: Jessa Salary: 10000
Name Mangling to access private members
Python doesn’t truly hide private members. Instead, it uses name mangling: any member name inside a class that starts with two underscores (and doesn’t end with two underscores) is automatically renamed to _ClassName__member. For example, __salary in the Employee class is stored as _Employee__salary.
That’s why emp.__salary fails, but emp._Employee__salary works. Name mangling applies only to private (double underscore) members, not to protected (single underscore) members.
Example: Access private member
Output
{'name': 'Jessa', '_Employee__salary': 10000}
Salary: 10000
Note: The main purpose of name mangling is to avoid accidental name clashes in subclasses, not to provide security. If a parent class and a child class both use self.__secret, they are stored as two different attributes, _Parent__secret and _Child__secret, so the child doesn’t overwrite the parent’s value. Accessing _ClassName__member from outside works, but you should avoid it in real code.
Protected Member
Protected members are meant to be accessed within the class and its subclasses. To define a protected member, prefix the member name with a single underscore _.
Use protected data members with inheritance, when you want child classes to use a member but don’t want it to be part of the class’s public interface.
Important: Python doesn’t enforce protected access. A single underscore is only a convention that tells other programmers “this is internal, don’t use it from outside.” As the last line of the following example shows, the member is still accessible from outside the class.
Example: Protected member in inheritance.
Output
Employee name : Jessa Working on project : NLP Project: NLP
Getters and Setters in Python
To control how data is read and changed, we use getter and setter methods. A getter method reads a data member, and a setter method modifies it. Together they ensure data encapsulation.
In Python, private variables aren’t truly hidden, as they are in some other languages. Getters and setters are typically used:
- To avoid direct access to private variables
- To add validation logic before setting a value
Example
Output
Name: Jessa 14 Name: Jessa 16
Let’s take another example that uses encapsulation to hide information and validate a value before changing an object’s attribute (data member).
Example: Information hiding and conditional logic for setting an object’s attributes
Output:
Student Details: Jessa 10 Invalid roll no. Please set correct roll number Student Details: Jessa 25
The Pythonic Way: @property
Writing get_age() and set_age() works, but in Python the preferred way is the built-in @property decorator. It turns methods into a getter and a setter that are used like a normal attribute (stud.age), while still running your validation code behind the scenes.
Output:
Name: Jessa Age: 14 Name: Jessa Age: 16 Error: Age must be between 0 and 120
Because the validation also runs in __init__() (through self.age = age), an invalid age can’t get into the object at all. If you define only the @property getter and no setter, the attribute becomes read-only.
Data Hiding vs. Encapsulation
These two terms are related but not the same:
- Encapsulation is about bundling: putting data and the methods that work on that data together in one unit (a class).
- Data hiding is about restricting access: preventing outside code from directly reading or changing an object’s internal data, using private or protected members.
Data hiding is one way to achieve encapsulation. A class with only public members is still encapsulated, because it bundles data and methods, but it doesn’t hide its data.
Advantages of Encapsulation
- Data protection: Encapsulation helps protect an object’s data from accidental or invalid changes. Private and protected members, together with setters that validate input, keep the object in a valid state.
- Data hiding: Users of a class don’t need to know what happens behind the scenes. They just call a setter (or assign to a property) to change data, and a getter to read it. How those methods work internally is hidden from them.
- Simplicity and maintainability: You can change a class’s internal implementation without breaking code that uses it, as long as the public interface stays the same. This keeps classes loosely coupled.
- Readability: Bundling data and methods within a class makes code more organized, readable, and maintainable.
