调用元类基数时,object .__ init __()不带参数 [英] When calling the metaclass bases, object.__init__() takes no parameters
本文介绍了调用元类基数时,object .__ init __()不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用这种单例方法时:
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
我遇到一个错误:
调用元类基数时出错 对象. init ()不带参数
Error when calling the metaclass bases object.init() takes no parameters
我不确定,我是否正确理解参数采用的是__init__
方法:name, bases, dict
.而实际上-我的错误/理解力在哪里?
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?
推荐答案
元类是从type
而不是object
派生的.
Metaclasses derive from type
, not object
.
这篇关于调用元类基数时,object .__ init __()不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文