Dynamically Update Class Definitions?

Chris Spencer usenet.20.evilspam at spamgourmet.com
Sat Nov 12 00:21:00 EST 2005


Alex Martelli wrote:
> <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.

Couldn't I just loop over gc.get_referrers(cls), checking for instances 
of the class object? Since class instances refer to their class, the gc 
seems to be doing the exact same thing as Hudson's fancy metaclass. Or 
am I missing something?

Chris



More information about the Python-list mailing list