假设我们有一个k类型的对象class A.我们定义了第二个class B(A).将对象"转换" k为class B并保留所有数据的最佳做法是什么k?
我正在尝试将函数从任意“基”类复制到新对象中。但是,此示例代码出现以下错误。
class my_base:
def print_hey():
print("HEY")
def get_one():
print(1)
class my_ext:
def __init__(self, base):
methods = [method for method in dir(base) if callable(getattr(base, method))]
for method in methods:
setattr(self, method, getattr(base, method))
me = my_ext(my_base)
me.get_one()
Run Code Online (Sandbox Code Playgroud)
上面在调用时出现此错误setattr。
TypeError: __class__ assignment only supported for heap types or ModuleType subclasses
Run Code Online (Sandbox Code Playgroud)
如果在定义以上内容后在提示中键入该语句,则该语句有效。