In this tutorial, we are going to learn about the private variables in Python classes.
Python doesn't have the concept called private variables. But, most of the Python developers follow a naming convention to tell that a variable is not public and it's private.
We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc..,.
As we already said the variables whose names start with a double underscore are not private. We can still access. Let's see how to create private type variables and then we will see how to access them.
# creating a class class Sample: def __init__(self, nv, pv): # normal variable self.nv = nv # private variable(not really) self.__pv = pv # creating an instance of the class Sample sample = Sample('Normal variable', 'Private variable')
We have created a class and its instance. We have two variables one is normal and the other is private inside the __init__ method. Now, try to access the variables. And see what happens.
Example
# creating a class class Sample: def __init__(self, nv, pv): # normal variable self.nv = nv # private variable(not really) self.__pv = pv # creating an instance of the class Sample sample = Sample('Normal variable', 'Private variable') # accessing *nv* print(sample.nv) # accessing *__pv** print(sample.__pv)
Output
If you run the above code, then you will get the following output.
Normal variable --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-13-bc324b2d20ef> in <module> 14 15 # accessing *__pv** ---> 16 print(sample.__pv) AttributeError: 'Sample' object has no attribute '__pv'
The program displayed the nv variable without any errors. But we got AttributeError when we try to access the __pv variable.
Why do we get this error? Because there is no any attribute with the variable name __pv. Then what about self.__pv = pv statement in the init method? We'll discuss this in a bit. First, let's see how to access the __pv variable.
We have access any class variable whose name startswith a double underscore as _className\_variableName_. So, in or example it is_Sample\_pv_. Now, access it using the _Sample\_pv_ name.
Example
# creating a class class Sample: def __init__(self, nv, pv): # normal variable self.nv = nv # private variable(not really) self.__pv = pv # creating an instance of the class Sample sample = Sample('Normal variable', 'Private variable') # accessing *nv* print(sample.nv) # accessing *__pv** using _Sample__pv name print(sample._Sample__pv)
Output
If you run the above code, then you will get the following result.
Normal variable Private variable
Why the name of the variable __pv has changed?
In Python, there is a concept called name mangling. Python changes the names of the variables that start with a double underscore. So, any class variable whose name starts with a double underscore will change to the form _className\_variableName_.
So, the concept will apply for the methods of the class as well. You can see it with the following code.
Example
class Sample: def __init__(self, a): self.a = a # private method(not really) def __get_a(self): return self.a # creating an instance of the class Sample sample = Sample(5) # invoking the method with correct name print(sample._Sample__get_a()) # invoking the method with wrong name print(sample.__get_a())
Output
If you run the above code, then you will get the following result.
5 --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-19-55650c4990c8> in <module> 14 15 # invoking the method with wrong name ---> 16 print(sample.__get_a()) AttributeError: 'Sample' object has no attribute '__get_a'
Conclusion
The purpose of using a double underscore is not to restrict from accessing the variable or method. It is to tell that the particular variable or method is meant to be bound inside the class only. If you have any queries in the tutorial, mention them in the comment section.