Class mutation

Thomas Heller thomas.heller at ion-tof.com
Thu Sep 27 07:38:14 EDT 2001


"Dale Strickland-Clark" <dale at riverhall.NOSPAMco.uk> wrote in message news:8626rt838oe59d1njrtrisccm0oe02shvf at 4ax.com...
> Lets say we have a class 'Job' and another class derived from that
> 'ActiveJob'.
>
> I'd like to mutate a Job object into the extended ActiveJob object
> without re-assigning all the attributes.
>
class Job:
    def work(self):
        print "I'm inactive"

class ActiveJob(Job):
    def work(self):
        print "Ok, later"

job = Job()

print job
job.work()

job.__class__ = ActiveJob

print job
job.work()

prints:

<__main__.Job instance at 007B5EBC>
I'm inactive

<__main__.ActiveJob instance at 007B5EBC>
Ok, later

Thomas





More information about the Python-list mailing list