In this python tutorial, you will learn about python access modifiers with examples. Here we will check:
- What is python access modifiers
- Public access modifier in python
- Private access modifier in python
- Protected access modifier in python
- python access modifiers complete example
Python Access Modifiers
- Python access modifiers are used to modify the default scope of a variable. There are three types of access modifiers in python and they are – Public, Private, and Protected. In python, we use underscores “_” symbol to specify the access modifier for specific data members and member functions in the class.
- Python access modifiers play an important role to protect the data from unauthorized access. When inheritance is implemented there is a huge risk for the data to get destroyed due to the transfer of unwanted data from the parent class to the child. That is why the access modifier is used.
Let us discuss in details the type of access modifiers in Python:
- Public
- Private
- Protected
Public access modifier in python
In the case of the Public access modifier in python, all the members in python class are public by default. Any member declared as public can be accessed from outside the class through an object.
Example:
class Teacher:
def __init__(self,name,salary):
self.name=name
self.salary=salary
t1=Teacher("Simon", 12500)
print(t1.name)
- After writing the above code (public access modifier in python), Ones you will print “t1.name” then the output will appear as a “ Simon”.
- Here, the member variable of the class is public by default so we can access the “Teacher” class attribute, and also we can modify their values.
You can refer to the below screenshot for public access modifier in python.
In public access modifier we can also modify their value here we will see with the help of an example.
Example:
class Teacher:
def __init__(self,name,salary):
self.name=name
self.salary=salary
t1=Teacher("Simon", 12500)
t1.name="Sam"
print(t1.name)
After writing the above code (public access modifier in python), Ones you will print “t1.name” then the output will appear as a “ Sam”. Here, we get the modified values, so in this way, we can modify any values.
You can refer to the below screenshot.
Private access modifier in Python
Now, let us discuss about private access modifiers in Python.
A double underscore “__” makes the variable private as well as secure and the members of the class which is declared private are accessible within the class. Also, it is not possible to access them outside the class because it will throw an error.
Example:
class Teacher:
def __init__(self,name,salary):
self.__name=name
self.__salary=salary
t1=Teacher("Simon", 12500)
print(t1.__name)
- After writing the above code (private access modifier in python), Ones you will print “t1.__name” then the output will throw an error as “ AttributeError: ‘Teacher’ object has no attribute ‘__name’ ”.
- Here, we tried to access the private members outside class so, we get this error.
You can refer to the below screenshot for private access modifier in python.
So, to access the private members of a class we have name mangling of private variables. Every member with a double underscore will be changed to ” object._class__variable “ and then it can be accessed from outside the class.
Example:
class Teacher:
def __init__(self,name,salary):
self.__name=name
self.__salary=salary
t1=Teacher("Simon", 12500)
print(t1._Teacher__name)
After writing the above code, Ones you will print “ t1._Teacher__name ” then the output will appear as “ Simon ”. Here, we can access the private members outside the class by printing object._class__variable.
You can refer to the below screenshot to access private members in python.
Protected access modifier in python
Now, let us discuss on protected access modifier in Python with an example.
The data member of the class is declared protected by adding a single underscore “_” and this prevents it from access. The protected members of the class can be accessed by other members within the class also it can be accessible to its class derived from it.
Example:
class Teacher:
def __init__(self,name,salary):
self._name=name
self._salary=salary
t1=Teacher("Simon", 12500)
print(t1._name)
- After writing the above code (protected access modifier in python), Ones you will print “t1._name” then the output will appear as “ Simon ”.
- Here, we made the class variable name and salary protected by giving underscores. We can access the protected members from the outside class with the help of an object.
You can refer to the below screenshot for protected access modifier in python.
Python access modifiers program example
Now, let us see an example of Python access modifiers with a complete program.
As we have seen all the Python access modifiers separately with an example, now let’s combine all the three into the program to see how it works.
Example:
class Teacher:
val1 = None
_val2 = None
__val3 = None
def __init__(self, val1, val2, val3):
self.val1 = val1
self._val2 = val2
self.__val3 = val3
def dispPublicMembers(self):
print("This is public member: ", self.val1)
def _dispProtectedMembers(self):
print("This is protected member: ", self._val2)
def __dispPrivateMembers(self):
print("This is private member: ", self.__val3)
def accessPrivateMembers(self):
self.__dispPrivateMembers()
class Child(Teacher):
def __init__(self, val1, val2, val3):
Teacher.__init__(self, val1, val2, val3)
def accessProtectedMembers(self):
self._dispProtectedMembers()
obj1 = Child("Hello", "Simon", 100000)
obj1.dispPublicMembers()
obj1.accessProtectedMembers()
obj1.accessPrivateMembers()
- After writing the above code in python, the output will appear as “ Hello Simon 100000 ”.
- Here, we have parent class as “Teacher” and derived class as “Child”, and the private members are accessed by making it public member function “def accessPrivateMembers” and it can access private members of the class.
- Also, we have a “Child” class and it inherits the properties of the parent class and also it can access the protected member’s function of “Teacher” class which is the parent class.
You can refer to the below screenshot program with the use of all the above three access modifiers python.
You may like the following Python tutorials:
In this tutorial, we learned about access modifier in python and also we have seen how to use it with an example like:
- Python Access Modifiers
- Public access modifier in python
- Private access modifier in python
- Protected access modifier in python
- A complete example of Python Access Modifiers
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.