I'll refer you to Python's documentation on name mangling.
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.
So this raises an AttributeError
because the __test
methods has been implicitly renamed:
class E:
@staticmethod
def __test():
print("foo")
E.__test() # AttributeError
But can be made to work by using the mangled name _E__test
:
class E:
@staticmethod
def __test():
print("foo")
E._E__test() # prints 'foo'
Notice that I had to add a staticmethod
decorator due to your method not taking a self
argument. This is out of scope, but you can read about it here, here and here.