When calling the metaclass bases, object.__init__(

2019-09-16 18:29发布

问题:

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?

回答1:

Metaclasses derive from type, not object.