I'm following this link and trying to make a singleton class using Metaclass. But, I want to make some internal tweaks to this singleton class and want the users to use another class (let's call it MySingleton(__Singleton)
). So I decided to make it private but it gives the following error.
My sole purpose is to prevent __Singleton
from being used outside. How can I achieve this?
On a separate note, is it a good practice to use double underscore with classes?
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