Hooks, aspect-oriented programming, and design by contract

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Tue Jan 22 12:13:05 EST 2002


Alex Martelli wrote:

> I also point out that I don't know how to switch metaclasses 
> dynamically,
> temporarily and "retroactively" (i.e. so that already-existing instances
> are also affected), as requested by the original poster.  I don't even
> know if such a metaclass-switch scheme is possible at all.

How retroactive do you want to be?  If you know in advance you might want 
to do this kind of thing, you can return a trivial subclass of the "real" 
class.  Then, to change all instances, reassign the __bases__.  It should 
work with old-style classes at least.  So if you know in advance you want 
to do dynamic switching, use old-style classes.  Even with the new ones, 
could you write a wrapper that delegates to a dynamically assigned class?

If an old-style class was created without you knowing you wanted it to be 
dynamic, you can still reassign all the attributes.  Presumably, the whole 
point of new-style classes is to stop you doing that kind of thing.


                      Graham




More information about the Python-list mailing list