Access Modifiers in Python
Access modifiers, as the name suggests, are used for putting some restrictions on the access of data members and methods of the class. If you are already familiar with some other programming language like Java, you might be aware of the different access modifiers, and what they do for us. But here, we are going to look at the access modifiers in Python from scratch. Just understand that the access modifiers tell how things can be accessed.
Access Modifiers in Python
In python, we have some access modifiers, like public, protected, and private, and we are going to have a brief look at them one by one.
One thing is that the access modifiers help us a lot when it comes to protecting the data from unauthorized access. Many times, we might need to protect the data in the program, from being altered, or accessed, since there is a risk of data manipulation. So, using the right access modifiers over the methods, or the variables, can help a lot, and can be used as per the requirements.
Public Access Modifier in Python
When the data members or variables are public, they can be accessed from anywhere, outside the class. The thing is that by default, the data members and the methods are public in a python program. Let’s have a look at an example, through which, we can understand the public access modifier.
As you can see, we have the methods, and the members in the class, as public. Understand that by default, all the members and the methods in the class are public, which means that they can be accessed from anywhere outside the class. Now, Let’s have a look at another access modifier, which is protected.
Protected Access Modifier in Python
When we use the protected access modifier over some variable, or over some method, it can be accessed within the same class, and outside the class, it can only be accessed by the subclass.
So, if we want to make some variable, or some method as protected, we need to just put an underscore (“_”) before the name of the method, or the variable.
Now, Let’s have a look at a simple program, through which, we can understand the protected access modifier. First, have a look at the program and then we would try to explain it –
As you can see, we have created a protected variable, which is _salary in the Employee class, and we have created another class, which is the class Manager, which is the subclass of the Employee class. We are trying to use the protected variable with the object of the Manager class, which is the subclass of Employee. The basic thing that we are trying to achieve is that the salary of an Employee, should be only accessible to some Employees, and in this case, due to inheritance, the Manager is also an Employee, so the Manager object is also able to access the salary.
Now, Let’s have a look at creating a protected method. Similar to creating a protected variable, before the name of the method, we have to add an underscore, and the method will be protected. Here as well, the protected method is going to be accessible in the same class, and in the subclass. Let’s have a look at an example, through which, we would easily understand creating the protected methods.
As you can see, we have a protected method in the Employee class, which is the _employeeMethod, in which, we have written a simple print statement. You can also see that we have created another class, Manager, which is the subclass of the Employee class. Now, we are trying to call the _employeeMethod with the Manager class object, and due to inheritance, we are able to access it.
You can try omitting the inheritance and try to run the program again, and you would find that we are not able to use the _employeeMethod then. But since there is a relationship between the Manager and the Employee, we are able to do so. You can also try running the program and observing the output.
So, whenever we are required to make the variable, or the method in the class, as protected, we can just put one underscore, before the name of the variable or the method. Now, Let’s head toward the private access modifier.
Private Access Modifier in Python
As the name suggests, the private access modifier helps us make our variables and methods in the class private, which simply means that they won’t be accessible outside the class. So, in order to make the variables or the methods private, we need to put two underscores (“__”) before the name of the variable or the method name. The thing is that even outside the class, we won’t be able to use the private members directly.
Let’s have a look at an example, which tells us about the private variable.
As you can see, here, we have an Employee class, and we have a private variable, which is __privateStuff. We create an Employee class object and try to access the private instance variable outside the class, and we are unable to do so. On the other hand, when we are trying to use the protected member, we are able to use it. You can try running the program and observing the output as well.
So, the thing is that the private member cannot be also used outside the class directly. The same is with the private method. Let’s have a look at a simple example, demonstrating the private access modifier with the method.
In the above program, we have a simple class Employee, in which, we have a private method, called __privateMethod. We are creating an object of the Employee class, and then we are trying to access the private method outside the class, which cannot be done. In this case, we run into errors.
Now, we know that we cannot use the private variables and the private methods, outside the class, you can try to do so, but the thing is that if we are required to access them, what can be done?
Well, in order to access the private variable or the private method, we can do some things, that we are going to discuss now.
Accessing the private variables and the private methods
Now we are familiar with the fact, that we cannot access private things outside the class. But now we are more interested in knowing what can be done if we want to access them. We can do some things, like –
- Creating public methods, through which we can access private stuff.
- Through name mangling.
First of all, Let’s have a look at creating a public method, to access the private things from the class.
The thing is that here, we are going to create a public method in the class, and then within that method, we are going to access the private variable. The thing is that we can access the private variable inside the class, so this would be valid, and we would return the value from the public method. Let’s have a look at the program now, which tries to demonstrate the same thing.
As you can see, in the above class Employee, we have a private instance variable, and also, we are having a public method, in which, we are returning the value of the private variable for some instance. The thing is that since the private variable can be accessed within the class, we can access it within that method, and in that method, we are returning the value of that variable, so we are getting the desired output. You can also try to run the program and have a look at the output.
Now, Let’s have a look at something called name mangling, through which, we can access the private member of the class.
The thing is that, when we are making some variable or method private, the name will be mangled, which means that the name of the variable, or the method, would be modified. So, simply if we have a private variable __privateStuff from the previous example, it will be modified to the name as _classname__variableName. So, you can access it that way. Let’s have a look at an example, which tries to demonstrate the same thing.
As you can see in the above program, we have a class Employee, and there we have a private variable. As you can see how we are accessing the private variable outside the class. You can access it either way,
So, this was much about the access modifiers in python. We can make use of the different access modifiers as and when required.