您可以很容易地从Python 3.x模拟相同的效果:
class Final(type): def __new__(cls, name, bases, classdict): for b in bases: if isinstance(b, Final): raise TypeError("type '{0}' is not an acceptable base type".format(b.__name__)) return type.__new__(cls, name, bases, dict(classdict))class C(metaclass=Final): passclass D(C): pass
Traceback (most recent call last): File "C:Tempfinal.py", line 10, in <module> class D(C): pass File "C:Tempfinal.py", line 5, in __new__ raise TypeError("type '{0}' is not an acceptable base type".format(b.__name__))TypeError: type 'C' is not an acceptable base type
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)