In this Python tutorial, you will learn about Python access modifiers with examples. So. let us start with access modifiers in Python.
What are Access Modifiers in Python?
- 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 detail 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 the 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:
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, 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 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:
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:
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 being access. The protected members of the class can be accessed by other members within the class also it can be accessible to the 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:
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 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 the parent class as “Teacher” and the 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 the “Teacher” class which is the parent class.
You can refer to the below screenshot:
You may like the following Python tutorials:
- Get File Extension from File Name in Python
- How to create empty set in Python
- Python check if a variable is an integer
In this tutorial, we learned, access modifiers in Python. and also, we saw some examples of Python access modifiers.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.