Inside the class, the identifier __Singleton
is getting mangled. You end up having problems because name mangling only happens inside classes (not outside). So __Singleton
as a class name means something different than __Singleton
when you are inside a class suite.
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, as long as it occurs within the definition of a class.
Note that the primary reason for mangling is because it
... is helpful for letting subclasses override methods without breaking intraclass method calls.
Also:
... to avoid name clashes of names with names defined by subclasses
As such, there really isn't any reason to have a class with leading double underscores in the name (there is no chance of intraclass method calls having conflicts with class names). A single leading underscore is a good enough signal to users that they shouldn't use that class:
... a name prefixed with an underscore (e.g. _spam
) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
I wouldn't advise it, but if you really want it to work, you can probably use globals
to look up the class:
class __Foo(object):
def __init__(self):
super(globals()['__Foo'], self).__init__()
f = __Foo()
print f