10

I saw a lot of methods of making a singleton in Python and I tried to use the metaclass implementation with Python 3.2 (Windows), but it doesn"t seem to return the same instance of my singleton class.

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class MyClass(object):
    __metaclass__ = Singleton

a = MyClass()
b = MyClass()
print(a is b) # False

I use the decorator implementation now which is working, but I'm wondering what is wrong with this implementation?

Elazar
  • 20,415
  • 4
  • 46
  • 67
Lord Gamez
  • 299
  • 7
  • 15
  • 2
    I know this thread is old, but why were you using Python2 syntax if you were programming in Python3? – Charles D Pantoga Feb 02 '16 at 15:39
  • Is anyone else bothered that the class is allowed to take arguments? What behavior is defined when a singleton is "instantiated" with two different sets of arguments? – Noctis Skytower Jun 06 '19 at 19:21

1 Answers1

21

The metaclass syntax has changed in Python3. See the documentaition.

class MyClass(metaclass=Singleton):
    pass

And it works:

>>> MyClass() is MyClass()
True
Elazar
  • 20,415
  • 4
  • 46
  • 67
  • 1
    Your name is very appropriate. All the tutorials I found are very nice but were never updated for Python 3. You just truncated to a couple of hours what might have taken days... Thanks. – Mad Physicist Mar 04 '16 at 20:14
  • You left the parent class (object) out. Is it still valid and is there a reason to use something like `class MyClass(object, metaclass=Singleton)`? – Seppo Enarvi Apr 27 '16 at 11:44
  • Iff you want to subclass something other than `object`. With `object` it's redundant. – Elazar Apr 27 '16 at 13:10
  • I have to do `class MyClass (__metaclass__=Singleton):`, that is double underscores on either side of metaclass or else I get `The word metaclass is not correctly spelled`. – Al Lelopath May 31 '19 at 19:59