Dynamically Update Class Definitions?

Alex Martelli aleax at mail.comcast.net
Fri Nov 11 22:36:46 EST 2005


<chrisspen at gmail.com> wrote:

> Is there a way to loop through all instantiated objects and update
> their classes when a source file changes? I know about Michael Hudson's
> method
> (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164), but
> you have to modify all your classes to subclass AutoReloader. Is there
> something less intrusive (visitor pattern?) that you can use like
> update(old_class, new_class) to automagically do the work?

If you're in no hurry, you COULD loop over all of gc.get_objects(),
identify all those which are instances of old_class and "somehow" change
their classes to new_class -- of course, x.__class__ = new_class may
well not be sufficient, in which case you'll have to pass to update a
callable to do the instance-per-instance job.


Alex



More information about the Python-list mailing list