Class __repr__ of a metaclass, not a class
我知道使用元类定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Meta(type): def __repr__(cls): return 'Person class: {}'.format(cls.__name__) class Person(metaclass=Meta): def __init__(self, name, age, job): self.name = name self.job = job self.age = age def __str__(self): return 'Person: {}, {}, {}'.format(self.name, self.age, self.job) class Employee(Person): def __init__(self, name, age): super(Employee, self).__init__(name, age, 'employee') class Manager(Person): def __init__(self, name, age): super(Manager, self).__init__(name, age, 'manager') m = Manager('bob', 79) e = Employee('stephen', 25) |
如预期,
实际上,没有什么可以阻止您为元类本身编写带有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | In [2]: class MM(type): ...: def __repr__(cls): ...: return f"<metaclass {cls.__name__}" ...: In [3]: class M(type, metaclass=MM): ...: def __repr__(cls): ...: return f"<class {cls.__name__}>" ...: In [4]: class O(metaclass=M): ...: pass ...: In [5]: o = O() In [6]: o Out[6]: <<class O> at 0x7ff7e0089128> In [7]: O Out[7]: <class O> |
1 2 | In [8]: repr(M) Out[8]: '<metaclass M' |
(这里令人困惑的是,
找到了一个简单的解决方案。由于我的类结构(继承树)如下,我只需要返回下一个类:
1 2 3 4 5 | MetaType: metaclass with __repr__ | Type: base class | builtins: e.g. String, Number |
所以我输入的代码是:
1 2 3 4 5 6 | t = type(self.parse(args['object'])) # where 'self.parse' is the parsing method for the argument to my function # where args['object'] is the object whose type is being returned if t == MetaType: return Type return t |