fields['key'] = 'value'
元类机器启动之前的运行。
class foo(object):
var1 = 'bar'
def foobar(self):
pass
当 python 命中该class
语句时,它会进入一个新的本地命名空间。
它评估var1 = 'bar'
语句。这相当于locals()['var1'] = 'bar'
然后它评估该def foobar
语句。这相当于locals()['var'] = the result of compiling the function
然后它locals()
连同类名、继承的类和元类一起传递给元类 方法__new__
。在示例中,元类是简单的type
。
__new__
然后它退出新的本地命名空间并将从外部命名空间返回的类对象与 name 粘贴在一起foo
。
您的代码在您使用时有效,因为已经创建A.fields
了该类,因此上述过程已在您的安装中执行。A
Meta
fields
A
您不能使用super().fields
,因为类名B
未定义为在 super 运行时传递给 super。也就是说,您需要它,super(B).fields
但在创建类之后B
定义 。
更新
这是一些代码,可以根据您对我对该问题的评论的回复来执行您想要的操作。
def MakeFields(**fields):
return fields
class Meta(type):
def __new__(mcs, name, bases, attr):
for base in bases:
if hasattr(base, 'fields'):
inherited = getattr(base, 'fields')
try:
attr['fields'].update(inherited)
except KeyError:
attr['fields'] = inherited
except ValueError:
pass
return type.__new__(mcs, name, bases, attr)
class A(metaclass=Meta):
fields = MakeFields(id='int',name='varchar')
class B(A):
fields = MakeFields(count='int')
class C(B):
pass
class Test(object):
fields = "asd"
class D(C, Test):
pass
print C.fields
print D.fields