簡體   English   中英

是否可以從基類的實例創建派生類型的實例

[英]Is it possible create instance of derived type from instance of base class

像這樣的東西:

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

我想使實例派生自Refrence,但行為與往常一樣...是否可能? 提前謝謝!

如果我明白你想做什么...

class Reference(object): pass

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM