How to create a private variable in Python?
If you have worked with languages like C#, JAVA or C++, you are familiar with the concept of private, protected and public fields. Although Python has great support for inheritance and polymorphism, the OO-concept encapsulation is not so well supported. What are your options?
In a language like C# you can make members public or private like this:
public ID = 0; private _name = "Pipo"; protected _salary = 2000;
In Python, variables in classes are public by default. Can you make them private?
Python does not know the concept of private variables. But you have a few options to mimic the behavior of a private variable.
1) Internal use with a single leading underscore
Variables and functions with a single leading underscore indicate internal use. What does that mean?
a = 10a is public
_b = 20_b is also public but is not imported when using the
from . import *syntax
_b = 20The underscore shows the intent of a private variable (according to certain style conventions)
a = 10 _b = 20
from config import * print(a) print(_b)
10 NameError: name '_b' is not defined
A single leading underscore does not change the visibility (also known as accessibility) but it does prevent from being imported using the * syntax.
The single leading underscore just suggests private.
2) Name mangling with a double leading underscore
Attributes prefixed with a double underscore will be name-mangled. Name mangling is a technique to give members a unique name to prevent conflicts with subclasses that want to use the same name for different purposes.
The python interpreter will rename the attribute
__name in class
_A__name, which prevents you from accessing it accidentally.
Does name mangling enable encapsulation? No, because is does not restrict access to such members.
class Employee: def __get_name(self): return "Pipo" e = Employee() print(e._Employee__get_name()) # accessible via name mangled attribute name print(e.__get_name()) # attribute is name mangled and thus causes an error
Pipo AttributeError: 'Employee' object has no attribute '__get_name'
Conclusion: private in Python is not possible
There is no way to make an attribute private in the sense of:
- Restrict exposure
- Restrict access
PEP8 describes the use of leading underscores for the purposes described above but makes no mention of private and public variables.
I’ve seen and used both single and double leading underscores to indicate visibility of attributes and methods.
Use whatever standards apply in your project. The most important thing to remember is that
__ will not make variables truly private in Python but they can indicate the intent optically.