Change the class of an object

Steve Purcell stephen_purcell at yahoo.com
Fri Apr 13 10:48:22 EDT 2001


Fernando Rodríguez wrote:
> 	Is it possible to change an object's class in runtime? O:-)

Yes, but you should be worried if that seems like a good idea. There's
usually a better solution. But if you insist:

    >>> class Flub:
    ...    def flubber(self):
    ...       print "Flub"
    ... 
    >>> class Ber:
    ...    def flubber(self):
    ...       print "Ber"
    ... 
    >>> f = Flub()
    >>> f.flubber()
    Flub
    >>> f.__class__ = Ber
    >>> f.flubber()
    Ber
    >>> 

-Steve

-- 
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo




More information about the Python-list mailing list