【发布时间】:2017-04-10 16:21:02
【问题描述】:
我正在尝试复制然后以编程方式修改一个类,但我遇到了 python 3 的魔法超级问题,例如以下
class Base():
def __init__(self):
print("Base init")
class Orginal(Base):
def __init__(self):
super().__init__()
print("Orginal init")
Modified = type(Orginal.__name__, Orginal.__bases__, dict(Orginal.__dict__))
Modified.f = lambda self: print("f")
m = Modified()
提高
TypeError: super(type, obj): obj 必须是类型的实例或子类型
所以我想知道,有什么方法可以帮助 super() 在通过 type() 创建的类中找到正确的 __class__ 单元格?
【问题讨论】:
-
我认为你的课程应该命名为“Original”。
标签: python inheritance metaprogramming typeerror super