Tuesday, December 21, 2010

Access Private Variables and Methods in Python

If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python's name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.
Private variables and methods are just name mangling in Python. You can access a private variable by using _ClassName__privateVarName.
class Foo:
def __init__(self):
self.__privateVar = 12

def __privateMethod(self):
return 'Private method invoked'

def greetings(self):
return 'Hello World!'

>>> foo = Foo()
>>> foo.greetings() #public method
'Hello World' #output
>>> foo._Foo__privateVar #private variable
12 #output
>>> foo._Foo__privateMethod() #private method
'Private method invoked' #output