1

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.

enter image description here

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?

Community
  • 1
  • 1
Pankaj Singhal
  • 12,388
  • 7
  • 37
  • 74

3 Answers3

3

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
mgilson
  • 264,617
  • 51
  • 541
  • 636
1

Every name inside a class definition with two leading underscores is mangled, so __Singleton becomes _Singleton__Singleton. To make clear, that some class is not supposed to be used publicly use one underscore.

Daniel
  • 39,063
  • 4
  • 50
  • 76
1

Python does not have private variables; they are all accessible externally.

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: 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.

The Python Cookbook provides a Singleton class that can be inherited by other classes to become Singletons.

Alexander
  • 87,529
  • 23
  • 162
  • 169