小编use*_*896的帖子

在哪种情况下调用父类中的多个元类?

奇怪的行为代码(用Python 2.7.3测试):

class Meta1(type):
   def __new__(mcl, name, bases, attrs):
        print "Hello Meta1.__new__ "
        return super(Meta1, mcl).__new__(mcl, name, bases, attrs)

class Meta2(type):
    def __new__(mcl, name, bases, attrs):
        print "Hello Meta2.__new__ "
        return super(Meta2, mcl).__new__(
            type, # looks to cause all strange behavior, but anyway pass type here, not mcl
            name, bases, attrs)

print "Declaring BaseClass1"
class BaseClass1(object):
    __metaclass__ = Meta1

print "-----------------------"
print "Declaring BaseClass2"
class BaseClass2(BaseClass1):
    __metaclass__ = Meta2

print "-----------------------"
print BaseClass2.__class__
Run Code Online (Sandbox Code Playgroud)

它的输出:

Declaring BaseClass1
Hello Meta1.__new__ 
-----------------------
Declaring …
Run Code Online (Sandbox Code Playgroud)

python inheritance metaclass python-2.x

5
推荐指数
1
解决办法
166
查看次数

标签 统计

inheritance ×1

metaclass ×1

python ×1

python-2.x ×1