【问题标题】:What is wrong with this metaclass implementation?这个元类实现有什么问题?
【发布时间】:2023-03-04 09:09:01
【问题描述】:

我并不是说我需要,我也看到了这个How do I create a simple metaclass?,但是这个简单的实现有什么问题:

>>>class one(object):
       def __init__(self):
           class self:pass

在这之后我做了:

>>>rat1=one()
>>>rat2=rat1()
TypeError: 'one' object is not callable

【问题讨论】:

    标签: python-2.7 metaclass


    【解决方案1】:

    元类派生自type,而不是object

    【讨论】:

    • 好的。所以我用“类型”替换了“对象”。但是它在第一次本身就给出了错误。 TypeError: type() 需要 1 或 3 个参数
    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 2011-09-28
    • 2011-12-14
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多