How to load new class definitions at runtime?

Alex Martelli aleaxit at
Tue Nov 16 07:54:11 CET 2004

SM <sjmaster at> wrote:

> aleaxit at (Alex Martelli) wrote in message
news:<1gn8ja3.7d3sbhuk94khN%aleaxit at>...
> > If you need [classes] to be _updatable_ at runtime, look at a recipe by
> > Michael Hudson in the cookbook -- I've updated it for the printed
> > version 2nd ed (since we do have __subclasses__ now, yay!) but that's
> > not done yet.  My vote for most useful custom metaclasses ever...
> That recipe is here:
> Is it correct that this metaclass solution does not handle changes to
> __init__, including new attributes that are bound there? I.e., it

It doesn't rerun __init__ nor any other method on existing instances.

> basically only deals with modification or addition or deletion of
> methods; if you need to add new attributes or have any other new code
> in __init__, you'll have to make new instances for all the old
> instances, or deal with it some other way. In which case would you be
> better off just using that other way from the start?

It's pretty trivial to enhance the recipe to run a per-instance or
per-subclass __make_old_into_new__ method, if that method exists, on all
existing old instances -- if you need to adjust the old instances'
states (==attributes), that strikes me as the best architecture.


More information about the Python-list mailing list