Dynamically Update Class Definitions?

Jean-Paul Calderone exarkun at divmod.com
Sat Nov 12 01:55:50 EST 2005


On Sat, 12 Nov 2005 06:24:57 GMT, Chris Spencer <usenet.20.evilspam at spamgourmet.com> wrote:
>Chris Spencer wrote:
>> Alex Martelli wrote:
>
>>> 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
>
>In fact, the following code seems to work, and doesn't require any
>modification to new-style class based code:

There are lots of cases where you cannot rebind the __class__ attribute.  For a comprehensive treatment of this idea (but still not a completely functionality implementation), take a look at <http://cvs.twistedmatrix.com/cvs/trunk/twisted/python/rebuild.py?view=markup&rev=11450>.  On another note, the usage of threads in this code is totally insane and unsafe.  Even for strictly development purposes, I would expect it to introduce so many non-deterministic and undebuggable failures as to make it cost more time than it saves.  You really want to stop the rest of the program, then update things, then let everything get going again.

Jean-Paul



More information about the Python-list mailing list