Python does not have any private variables like C++ or Java does. You could access any member variable at any time if wanted, too. However, you don't need private variables in Python, because in Python it is not bad to expose your classesclasses' member variables. If you have the need to encapsulate a member variable, you can do this by using "@property" later on without breaking existing client code.
In pythonPython, the single underscore "_" is used to indicate, that a method or variable is not considered as part of the public apiAPI of a class and that this part of the apiAPI could change between different versions. You can use these methods/variables and variables, but your code could break, if you use a newer version of this class.
The double underscore "__" does not mean a "private variable". You use it to define variables which are "class local" and which can not be easily overiddenoverridden by subclasses. It mangles the variables name.
class A(object):
def __init__(self):
self.__foobar = None # willWill be automatically mangled to self._A__foobar
class B(A):
def __init__(self):
self.__foobar = 1 # willWill be automatically mangled to self._B__foobar
self.__foobar's name is automatically mangled to self._A__foobar in class A. In class B it is mangled to self._B__foobar. So every subclass can define its own variable __foobar without overriding its parents variable(s). But nothing prevents you from accessing variables beginning with double underscores. However, name-mangling mangling prevents you from calling this variables /methods incidentally.
I strongly recommend you watch Raymond Hettinger's Python's class development toolkit from PyconPyCon 2013, which gives a good example why and how you should use @property and "__"-instance variables.
class Distance:
def __init__(self, meter):
self.meter = meter
d = Distance(1.0)
print(d.meter)
# prints 1.0
class Distance:
def __init__(self, meter):
# Customer request: Distances must be stored in millimeters.
# Public available internals must be changed.
# This would break client code in C++.
# This is why you never expose public variables in C++ or Java.
# However, this is pythonPython.
self.millimeter = meter * 1000
# In pythonPython we have @property to the rescue.
@property
def meter(self):
return self.millimeter *0.001
@meter.setter
def meter(self, value):
self.millimeter = value * 1000
d = Distance(1.0)
print(d.meter)
# prints 1.0