简体   繁体   中英

Is it possible create instance of derived type from instance of base class

Some thing like this:

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

I want to make instance to be derived from Refrence but behave as usual... Is it possible? Thx in advance!

If I understood what you want to do...

class Reference(object): pass

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM