Introduction to Python Private Variables
In general, private variables are those variables that can be visible and accessible only within the class they belong to and not outside the class or any other class. These variables are used to access the values whenever the program runs that is used to keep the data hidden from other classes. In Java, the private variables are declared private using keyword private which allows variables and methods with private can only access the members inside the class and outside the class and here private methods cannot be overwritten. In Python, actually, there is no such thing declared or anything that can be called a private member. But it can be done by adding two underscores ( __ ) at the beginning of any variable or method that needs to be declared private.
How Private Variables Work in Python?
Practically Python programming language doesn’t have any private member or variable such as in Java it is done using keyword private but in Python, it is done using two underscores
( __ ) at the beginning of any variable or method that needs to be declared private.. Let us see an example of how in Python we can use private variables and how are they declared.
Examples to Implement Python Private Variables
Below are the examples mentioned:
Example #1
Code:
class mainclass:
__privateVariable = 2020;
def __privateMethod(self):
print("I'm inside class mainclass that is this is private method")
def insideclass(self):
print("Private Variable: ",mainclass.__privateVariable)
self.__privateMethod()
foo = mainclass()
foo.insideclass()
Output:
Explanation: In the above program, we have a class defined as “main class” inside this we have variables and methods declared private using two underscores private variable as “__PrivateVariable” and private method as “__PrivateMethod” so these can be accessed with the same class “mainclass” but these cannot be accessed outside this class if we do then we get an error saying there is no such attribute in the class. This can be demonstrated in the below code with output:
Example #2
Code:
class mainclass:
__privateVariable = 2020;
def __privateMethod(self):
print("I'm inside class mainclass that is this is private method")
def insideclass(self):
print("Private Variable: ",mainclass.__privateVariable)
foo = mainclass()
foo.insideclass()
foo.__privateMethod()
Output:
Explanation: In the above program, we can notice that the method is private but we are trying to access it outside the class “mainclass” so it gives out the error saying the class declared has no instance of this attribute. So in general, all the variables and methods declared inside the class are public by methods by default but when we use underscores before them then they become private class members which they can be accessed only within the class they belong to but not to the outside classes. Such a process of making variables or method to private is known as data mangling or name mangling to avoid the ambiguity of names defined by the subclasses. This helps in overriding methods of subclasses without interrupting the intraclass method calls.
In Python, truly private is not supported so it is known as “weak internal use indicator”. So this is with an only single underscore ( _ ) which is used for declaring private variables, methods, functions, and classes in the module. So only with a single underscore also we can declare private variables and methods. Let us demonstrate this with below code:
Example #3
Code:
class Vehicle:
def _start_engine(self):
return "Start the Bike."
def run(self):
return self._start_engine()
if __name__ == '__main__':
bike = Vehicle()
print(bike._start_engine())
print"Bike has started."
bike.run()
print"Bike is running."
Output:
Explanation: So in the above code, we can private method “_start_engine” is used to make it private. But the single underscore is not used more because single underscore ( _ ) is just meant that you cannot access it from outside the class, where double underscore ( __ ) also means the same but it more strictly followed than single underscore.
The single underscore is not used much because if sometimes the private method declared using single underscore modifies ant class variables and if this method is called then there is a chance of it breaking the class behavior. As double underscore ( __ ) is most widely used and valid use case for declaring private class members as to avoid ambiguity to define names by subclasses, hence there is less support for such mechanism also known as name mangling, therefore, Python does not have private variables like in C++, Java to access any member variables at any time, because Python is not so bad to expose class member variables.
Conclusion
In general, any programming languages have 3 access modifiers that are private, public and protected. In Python, we also have all these access modifiers but the private access modifiers are not used or we can say it is not much supported in Python as it can expose well to private class member variables or methods. Private variables are those variables that can be accessed only inside the class that it belongs to, but it cannot be accessed by outside class. In Python, to declare private variables or methods single or double underscores are used while single is not much in practice because double underscore is used more as it supports the variables to be accessed within the class as well as it supports the name mangling mechanism.
Recommended Article
This is a guide to Python Private Variables. Here we discuss an introduction to Python Private Variables along with how does it work and respective examples. You can also go through our other related articles to learn more