Andrew Fount Andrew Fount - 3 years ago 118
Python Question

Double underscore in python

class A(object):
def __get(self):
pass

def _m(self):
return self.__get()


class B(A):
def _m(self):
return str(self.__get())

print(A()._m())
print(B()._m())


Why
print(A()._m())
prints
None
, but
print(B()._m())
raises
AttributeError: 'B' object has no attribute '_B__get'
?

I thought that double underscore prevents method overriding.

UPDATE

You write that
__get
is private.

Then why does the following work?

class A(object):
def __get(self):
pass

def _m(self):
return self.__get()


class B(A):
pass

print(A()._m())
print(B()._m())


Why does this code doesn't raise
AttributeError
and prints
None
two times?

Answer Source

Leading double underscore names are private (meaning not available to derived classes)

This is not foolproof. It is implemented by mangling the name. Python Documentation says:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

Thus __get is actually mangled to _A__get in class A. When class B attempts to reference __get, it gets mangled to _B__get which doesn't match.

In other words __plugh defined in class Xyzzy means "unless you are running as class Xyzzy, thou shalt not touch the __plugh."

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download