When calling the metaclass bases, object.__init__() takes no parameters

柔情痞子 提交于 2019-12-13 02:57:42

问题


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.



来源:https://stackoverflow.com/questions/9555402/when-calling-the-metaclass-bases-object-init-takes-no-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!