Private Variables and Methods in Python [duplicate] Private Variables and Methods in Python [duplicate] python python

Private Variables and Methods in Python [duplicate]


Please note that there is no such thing as "private method" in Python. Double underscore is just name mangling:

>>> class A(object):...     def __foo(self):...         pass... >>> a = A()>>> A.__dict__.keys()['__dict__', '_A__foo', '__module__', '__weakref__', '__doc__']>>> a._A__foo()

So therefore __ prefix is useful when you need the mangling to occur, for example to not clash with names up or below inheritance chain. For other uses, single underscore would be better, IMHO.

EDIT, regarding confusion on __, PEP-8 is quite clear on that:

If your class is intended to be subclassed, and you have attributesthat you do not want subclasses to use, consider naming them withdouble leading underscores and no trailing underscores. This invokesPython's name mangling algorithm, where the name of the class ismangled into the attribute name. This helps avoid attribute namecollisions should subclasses inadvertently contain attributes with thesame name.

Note 3: Not everyone likes name mangling. Try to balance theneed to avoid accidental name clashes with potential use byadvanced callers.

So if you don't expect subclass to accidentally re-define own method with same name, don't use it.


The double underscore. It mangles the name in such a way that it can't be accessed simply through __fieldName from outside the class, which is what you want to begin with if they're to be private. (Though it's still not very hard to access the field.)

class Foo:    def __init__(self):        self.__privateField = 4;        print self.__privateField # yields 4 no problemfoo = Foo()foo.__privateField# AttributeError: Foo instance has no attribute '__privateField'

It will be accessible through _Foo__privateField instead. But it screams "I'M PRIVATE DON'T TOUCH ME", which is better than nothing.


Double underscore. That mangles the name. The variable can still be accessed, but it's generally a bad idea to do so.

Use single underscores for semi-private (tells python developers "only change this if you absolutely must") and doubles for fully private.