Skip to content

What is Access modifier in Python (with Examples)

Access modifier in Python

In this tutorial we will learn about Access Modifier in Python . Before reading this tutorial we recommended to read about below tutorials –

What is Access modifier in Python

Any object oriented programming language (such as c ++, java, python) uses access modifier . The Access Modifier is used to restrict access to different methods or properties of a class. Python has three types of access modifiers, namely –Public access modifier, Protected access modifier and Private access modifier. Now we will discuss about each access modifier.

Public access modifier in Python

In Python, any defined method or property within a class is public by default. The public method or public property can be accessed by any object of that class or any class derived from that class or any class derived from that derived class and so on .

Protected access modifier in Python

In Python, if the name of any defined method or property in a class starts with underscore (_), then that method or property is protected. The protected method or protected property can be accessed by any object of that class or any class derived from that class or any class derived from that derived class and so on .

Private access modifier in Python

In Python, if the name of any defined method or property in a class starts with double underscore (__), then that method or property becomes private. Private method or private property can only be accessed within that class. We can not access private method or property directly using the object_name after creating object .

To access a private method or property with an object –

  • Either we have to use Name Mangling
  • Otherwise the private method or property must be accessed with the help of a public or protected method.

Examples of Access modifier in Python

Example 1:

# defining a class 
class Employee:
    def __init__ (self, name, salary, working_project):
        # protected object variable
        self._name = name
        # public object variable        
        self.salary = salary
        # private object variable 
        self.__project = working_project
        
    # accessing private property using public method    
    def get_project(self):
        print(self.__project)

In the above code, first a class named Employee is defined which have a public object variable named salary, a protected object variable named _name and a private object variable named __project. The class also has an object method Get_project by which we can access the private object variable __project .

# creating a object
manager = Employee("Arup", 25000, "building a website")

Then an object of that class named manager has been created.

# accessing public variable
print(manager.salary)
# changing public variable
manager.salary = 30000
print(manager.salary)
25000
30000

Then first the public object variable is printed, then the value of the public object variable is changed and printed again.

# accessing protected variable
print(manager._name)
# changing protected variable
manager._name = "Sourav"
print(manager._name)
Arup
Sourav

After that the protected object variable is printed, then the value of the protected object variable is changed and printed again.

Notice that there is no difference between public access modifier and protected access modifier except their name.

# accessing private variable 
print(manager.__project)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10376/2332106290.py in <module>
1 # accessing private variable
----> 2 print(manager.__project)

AttributeError: 'Employee' object has no attribute '__project'

Then an attempt was made to access the private object variable with the help of the object which raised AttributeError .

# accessing private variable using
# name mangling
print(manager._Employee__project)
# accessing private variable using
# another object method
manager.get_project()
building a website
building a website

After that we have accessed the private object variable __project using Name mangling and other object method get_project() .

Name Mangling in Python

When we define a private access modifier in Python, Python stores it by adding underscore (_) + class name before that access modifier so that the name of that access modifier is not accessed by any object of that class outside the class. This method is called Name mangling.

Since the name of the private object variable is __project and the name of the class is Employee, Python adds _Employee before __project and stores it as _Employee__project. So when we try to access the private object variable via _project, it shows AttributeError. But when we tried to access the private object variable via _Employee__project, it printed the private variable _project.

Example 2:

# define a class
class Programmer:
    def __init__ (self, name, age):
        self.name = name
        self.age = age
        
    # private method    
    def __set_salary (self, salary):
        self.salary = salary
        print(self.salary)
        
    def get_salary (self, salary):
        self.__set_salary(salary)
# define a derived class
class Python_programmer(Programmer):
    def __init__ (self,name,age,programming_language):
        super().__init__(name,age)
        # protected variable
        self._programming_language = programming_language
        
    # we can't access private method
    # of base class from derived class
    # if we call this method it will show error
    def set_salary (self, salary):
        self.__set_salary(salary)
# create object of derived class Python_programmer
rajkumar = Python_programmer("Rajkumar", 26, "Python")
print(rajkumar.name)
print(rajkumar.age)
print(rajkumar._programming_language)
# rajkumar.__set_salary(25000)  #error as private method
# rajkumar.set_salary(30000) # error
Rajkumar
26
Python
# accessing private method using public method
rajkumar.get_salary(30000)
# using name mangling
rajkumar._Programmer__set_salary(35000)
30000
35000

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply