织梦CMS - 轻松建站从此开始!

沙龙国际-沙龙365国际_沙龙国际网上娱乐

当前位置: 主页 > 沙龙国际网上娱乐 >

关于python的元类的问题

时间:2017-07-07 10:45来源:未知 作者:admin 点击:
最近在学习元类。 #python3# coding=utf-8class ModelMetaclass(type): def __new__(cls, name, bases, attrs): print("ainia") return type.__new__(cls, name, bases, attrs)class BBB: def __new__(cls, name, bases, attrs): print("BBBBB") retu

最近在学习元类。

#python3
# coding=utf-8
class ModelMetaclass(type):

    def __new__(cls, name, bases, attrs):
        print("ainia")
        return type.__new__(cls, name, bases, attrs)
class BBB:
    def __new__(cls, name, bases, attrs):
        print("BBBBB")
        return object.__new__(cls, name, bases, attrs)
class Model(metaclass=BBB):
    pass
class Model2(metaclass=ModelMetaclass):
    pass
#python2
# coding=utf-8
class ModelMetaclass(type):
    def __new__(cls, name, bases, attrs):
        print("ainia")
        return type.__new__(cls, name, bases, attrs)
class BBB(object):
    def __new__(cls, name, bases, attrs):
        print("BBBBB")
        return object.__new__(cls, name, bases, attrs)
class Model(object):
    __metaclass__ = BBB
    pass
class Model2(object):
    __metaclass__ = ModelMetaclass
    pass

以上代码另存为test.py,中运行时提示:

Traceback (most recent call last):
  File "test2.py", line 18, in 
      
      (责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容