是否可以从基类的实例创建派生类型的实例

2024-04-25 18:17:07 发布

您现在位置:Python中文网/ 问答频道 /正文

像这样的事情:

class Reference (object):
    pass
new_type = type ('{0}_refrence'.format (type (instance).__name__),
    (type (instance), Reference), {})
new_instance = new_type (instance)

我想让实例从引用中派生出来,但表现得像往常一样。。。 有可能吗? 提前谢谢!你知道吗


Tags: 实例instancenameformatnewobjecttypepass
1条回答
网友
1楼 · 发布于 2024-04-25 18:17:07

如果我明白你想做什么。。。你知道吗

class Reference(object): pass

instance = 2
new_type = type('{0}_reference'.format(instance.__class__.__name__), (instance.__class__, Reference), {})
new_instance = new_type(instance)

希望这有帮助。你知道吗

相关问题 更多 >