0

When I try to use this one approach of singleton:

class Singleton(object):                                                     
    def __init__(self, name, bases, dict):                                   
        super(Singleton, self).__init__(name, bases, dict)                   
        self._instance = None                                                

    def __call__(self):                                                      
        if self._instance is None:                                           
            self._instance = super(Singleton, self).__call__()
        return self._instance                                                


class NewClass(object):      
    __metaclass__ = Singleton

I got an error:

Error when calling the metaclass bases object.init() takes no parameters

I'm not sure, am I correctly understand what the arguments are takes __init__ method: name, bases, dict. And actually - where is my mistake/incomprehension?

I159
  • 29,741
  • 31
  • 97
  • 132

1 Answers1

7

Metaclasses derive from type, not object.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • This answer is incorrect. At least technically. The docs for [Python2](https://docs.python.org/2.7/reference/datamodel.html#customizing-class-creation) and [Python3](https://docs.python.org/3/reference/datamodel.html#determining-the-appropriate-metaclass) state that it is allowed not to inherit from type (although not recommended). In Python2 it can be any callable. – Elazar Nov 05 '17 at 04:42