Type casting a base class to a derived one?

Peter Otten __peter__ at web.de
Thu Jan 11 07:46:08 EST 2007


Frederic Rentsch wrote:

>    If I derive a class from another one because I need a few extra
> features, is there a way to promote the base class to the derived one
> without having to make copies of all attributes?
> 
> class Derived (Base):
>    def __init__ (self, base_object):
>       # ( copy all attributes )
>       ...
> 
> This looks expensive. Moreover __init__ () may not be available if it
> needs to to something else.

base_instance = Base(...)
base_instance.__class__ = Derived

Peter



More information about the Python-list mailing list