Python Access Modifiers(Public, Protected, Private)

Like other programming languages such as C#, JAVA, and C++, we don’t have access modifiers like public, private, and protected in python to define the access level restrictions for class members.

 

By default, all the python class members (variables, methods, etc.) are public. So, we can access all the class members outside of the class. To restrict access, you need to define the class members as private and protected.

 

To define the class variables or methods as private/protected, you need to prefix the single or double underscore (_) to the variable/method name.

Public Members in Python

As discussed by default, all the python class members are public, and we can access all the class members outside of the class based on our requirements.

 

Following is the example of creating a class with required data members and accessing those members outside of the class in python.

 

class user:
   id = 10
   name = "Suresh"

   def __init__(self, uid, uname):
     self.id = uid
     self.name = uname

   def userdetails(self):
     print("Id: {}, Name: {}".format(self.id, self.name))

u1 = user(2, "Rohini")
u1.id = 30
u1.name = "Trishi"
u1.userdetails()

When you execute the above python example, you will get the result as shown below.

 

Id: 30, Name: Trishi

Private Members in Python

Generally, the private members access is limited to the defined class. If you try to access the private members outside of the class, you will get an exception.

 

As discussed, we don’t have any built-in attributes to implement the private variables/methods in the python class. To create the private variables/methods in the python class, you need to prefix the double underscore (__) to the name of class variables or methods.

 

Following is the example of creating the private variables and methods in the python class.

 

class user:
   __id = 10
   __name = "Suresh"

   def __userdetails(self):
     print("Private Method")

u1 = user()
print(u1.__id)
print(u1.__name)
u1.__userdetails()

If you observe the above example, we prefixed double underscore (__) to the required variables and methods in the python class to define the private members.

 

When you execute the above python example, you will get the AttributeError exception as shown below.

 

AttributeError: 'user' object has no attribute '__id'
AttributeError: 'user' object has no attribute '__name'
AttributeError: 'user' object has no attribute '__userdetails'

Protected Members in Python

The protected members access limited to the defined class and the classes (sub-classes) that are derived from the defined class.

 

As discussed, in python we don’t have any built-in attributes to implement the protected variables/methods in the python class. To create the protected variables/methods in the python class, you need to prefix the single underscore (_) to the name of class variables or methods.

 

Following is the example of creating the protected variables and methods in the python class.

 

class user:

   def __init__(self, id, name):
     self._id = id
     self._name = name

   def _userdetails(self):
     print("Id: {}, Name: {}".format(self._id, self._name))

class Employees(user):
   pass

e1 = Employees(1, "Suresh")
print(e1._id)
print(e1._name)
e1._userdetails()

If you observe the above example, we prefixed a single underscore (_) to the required variables and methods in the python class to define the protected members. We are able to access the protected members from the derived classes.

 

The above example will return the result as shown below.

 

1
Suresh
Id: 1, Name: Suresh

This is how you can create the public, private, and protected variables, methods, etc., in python classes based on your requirements.