开发者社区 问答 正文

type()和type.__new__()创建类有什么区别呀?

class MetaB(type):
    def __new__(cls, name, bases, dct):
        print 'MetaB.__new__'
        return type.__new__(cls, name, bases, dct)
    def __init__(cls, name, bases, dct):
        print 'MetaB.__init__'

class metaclass(MetaB):
    def __new__(cls, name, this_bases, d):
        print "hahah"
        return MetaB(name, [], d)

c = type.__new__(metaclass,"hahah",(),{})

行这一段代码的时候,第十行print没有运行

展开
收起
珍宝珠 2019-11-08 11:24:57 910 分享 版权
1 条回答
写回答
取消 提交回答
  • 用python3

    type是object的实例
    object是所有python对象的基类

    2019-11-08 11:25:31
    赞同 展开评论
问答地址: