Python Access Modifiers
In most of the object-oriented languages access modifiers are used to limit the access to the variables and functions of a class. Most of the languages use three types of access modifiers, they are – private, public and protected.
Just like any other object oriented programming language, access to variables or functions can also be limited in python using the access modifiers. Python makes the use of underscores to specify the access modifier for a specific data member and member function in a class.
Access modifiers play an important role to protect the data from unauthorized access as well as protecting it from getting manipulated. When inheritance is implemented there is a huge risk for the data to get destroyed(manipulated) due to transfer of unwanted data from the parent class to the child class. Therefore, it is very important to provide the right access modifiers for different data members and member functions depending upon the requirements.
Python: Types of Access Modifiers
There are 3 types of access modifiers for a class in Python. These access modifiers define how the members of the class can be accessed. Of course, any member of a class is accessible inside any member function of that same class. Moving ahead to the type of access modifiers, they are:
Access Modifier: Public
The members declared as Public are accessible from outside the Class through an object of the class.
Access Modifier: Protected
The members declared as Protected are accessible from outside the class but only in a class derived from it that is in the child or subclass.
Access Modifier: Private
These members are only accessible from within the class. No outside Access is allowed.
Time for some Examples
In this section we will provide some basic code examples for each type of access modifier.
public
Access Modifier
By default, all the variables and member functions of a class are public
in a python program.
All the member variables of the class in the above code will be by default public
, hence we can access them as follows:
protected
Access Modifier
According to Python convention adding a prefix _
(single underscore) to a variable name makes it protected
. Yes, no additional keyword required.
In the code above we have made the class variables name and sal protected
by adding an _
(underscore) as a prefix, so now we can access them as follows:
Similarly if there is a child class extending the class Employee
then it can also access the protected member variables of the class Employee
. Let’s have an example:
Now let’s try to access protected member variable of class Employee
from the class HR
:
private
Access Modifier
While the addition of prefix __
(double underscore) results in a member variable or function becoming private
.
If we want to access the private member variable, we will get an error.
AttributeError: ’employee’ object has no attribute ‘__sal’
All in one Example
Now that we have seen each access modifier in separate examples, now let’s combine all that we have learned till now in one example:
Now the code above show the correct usage of public
, private
and protected
member variables and methods. You can try and change a few things a run the program to see what error those changes result into.
Â