I am doing the python koan (for python 2.6) and ecnountered something I don’t understand. One of the files has the following code in line 160:
class Dog(object):
def __password(self):
return 'password'
This
rover = Dog()
password = rover.__password()
results in an AttributeError. That is clear to me. (__password(self) is some kind of private method because of the leading two underscores).
But this
rover._Dog__password()
is a mystery to me. Could some one please explain to me how or why this works or better point me to the docs where this is described?
Double underscore :
So when you call the
__methodname, it’s exactly a call to_classname__methodname. The result is anAttributeErrorSingle underscore :
Variables in a class with a leading underscore are simply to indicate to other programmers that the variable should be private. However, nothing special is done with the variable itself.
Python documentation here :
Python private variables documentation
Complete post found here :
What is the meaning of a single- and a double-underscore before an object name?