Access Modifiers are used by various programming languages like C++, Java, Python, etc., that follow the object-oriented paradigm. Access Modifiers are used to modify the access of the class member variables and methods from outside the class. Encapsulation is a principle of OOPs that protects the internal data of the class with the help of access modifiers such as Public, Protected, and Private.
In the following tutorial, we will learn about the Access Modifiers and their types. We will also discuss how to use them in the Python programming language.
Python offers three levels of access modifiers:

Let us discuss these modifiers with the help of examples.
The member variables and methods of the public access modifier are accessible from anywhere in the program. By default, all the attributes and methods in Python classes are public unless explicitly modified.
Let us consider the following example of the public access modifier:
Output:
Lion Den Lion lives in Den
Explanation:
In the above snippet of code, we have defined a class called Animal. The Animal class has two member variables, name and home and a method display_data() which prints the member variable values. Both of these variables are public as no specific keyword (or convention) is assigned to them. We then instantiated the class as wild_animal. We then printed the values of the public attributes and called the method to print the details of the created object.
Several key features of Public Access Modifier in Python are as follows:
The member variables and methods of the protected access modifier are only accessible within the class where it is declared and their subclasses. In order to implement the protected field or method, the developer follows a particular naming convention, mostly by adding a prefix to the variable or function name.
Protected members are indicated by a single underscore (_variable). Note that the Python interpreter does not enforce this restriction like other languages; it is only designated for programmers as they would try to access it using a plain name instead of calling it with the help of the respective prefix.
Let us consider the following example of a protected access modifier.
Output:
Lion Den Lion lives in Den
Explanation:
In the above snippet of code, we have defined a class called Animal. The Animal class has two member variables, _name and _home and a protected method _display_data() which prints the member variable values. Both of these variables are protected, denoted by a single underscore (_) as a prefix. We then instantiated the class as wild_animal. We then printed the values of the protected attributes and called the method to print the details of the created object.
The member variables and methods of the private access modifier are only accessible within the class. Private access modifier is the most secure access modifier. Private members are indicated by double underscores (__) before the variable or method name. Python performs name mangling, meaning that it changes the name of the variable internally to prevent accidental access.
Let us consider the following example of the private access modifier.
Explanation:
In the above snippet of code, we have defined a class called Animal. The Animal class has two member variables, __name and __home and a private method __display_data() which prints the member variable values. Both of these variables are protected, denoted by a double underscore (__) as a prefix. We then instantiated the class as wild_animal. We then tried to print the values of the private attributes and called the method to print the details of the created object. However, doing this will raise an error.
We cannot directly access or modify the private attributes from outside the class in Python. The Name Mangling done by Python makes it difficult, but not impossible. It is strongly discouraged to do so, as it breaks encapsulation.
In name mangling, we can rename the private members of the class from __name to _ClassName__name. Let us consider the following example illustrating the use of name mangling in accessing the private members of the class.
Output:
Lion Den Lion lives in Den
Explanation:
In the above snippet of code, we have defined a class called Animal. The Animal class has two member variables, __name and __home and a private method __display_data() which prints the member variable values. Both of these variables are protected, denoted by a double underscore (__) as a prefix. We then instantiated the class as wild_animal.
We then used the name mangling where we have renamed the private attributes from __name and __home to _Animal__name and _Animal__home, respectively and printed their values for the users. We then called the method again using the name mangling to print the details of the created object. In this case, the values are printed successfully.
We will now look at the tabular comparison between public, protected and private access modifiers in Python.
| Access Modifier | Syntax | Accessibility | Example | Key Features |
|---|---|---|---|---|
| Public | varName | Public members are accessible from anywhere (inside and outside the class) | self.varName |
|
| Protected | _varName | Protected members are accessible within the class and subclass (It does not enforce this restriction; it simply provides a convention) | self._varName |
|
| Private | __varName | Private members are accessible only within the class (We can also apply name mangling to access the members) | self.__varName |
|
In the above tutorial, we have learned the basics of Object-Oriented Programming. We have also discussed the different principles of OOP. We then understood the concept of encapsulation in detail. We have learned that encapsulation, being a fundamental principle of OOP, helps protect the data and ensure controlled access to attributes and methods of the class. Python offers three access modifiers to implement encapsulation in programs. These access modifiers (public, protected, and private) allow programmers to manage data visibility and security.
We request you to subscribe our newsletter for upcoming updates.