Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private Variables and Methods in Python [duplicate]

Possible Duplicate:
The meaning of a single- and a double-underscore before an object name in Python

Which should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python?

like image 831
kurikintoki Avatar asked Aug 02 '10 05:08

kurikintoki


People also ask

How do you declare private variables and methods in Python?

In actual terms (practically), python doesn't have anything called private member variable in Python. However, adding two underlines(__) at the beginning makes a variable or a method private is the convention used by most python code.

Does Python have private variables?

In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object.

Can private variables be used in methods?

No, it is a syntax error, you can not use access modifiers within a method.

Which of the following is the correct way of accessing private variables in Python?

But there is a method in Python to define Private: Add “__” (double underscore ) in front of the variable and function name can hide them when accessing them from out of class. Python doesn't have real private methods, so one underline in the beginning of a method or attribute means you shouldn't access this method.


2 Answers

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 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.

Note 3: Not everyone likes name mangling. Try to balance the need to avoid accidental name clashes with potential use by advanced callers.

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

like image 195
Daniel Kluev Avatar answered Sep 26 '22 11:09

Daniel Kluev


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 problem  foo = 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.

like image 42
zneak Avatar answered Sep 24 '22 11:09

zneak