Swapping superclass from a module
Emanuele D'Arrigo
manu3d at gmail.com
Sat May 16 18:35:31 EDT 2009
On May 16, 8:17 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> # Insert Wedge into each subclass of modfoo.Base
> for subclass in modfoo.Base.__subclasses__():
> if subclass.__module__ != 'modfoo': continue
> attrs = dict(item for item in subclass.__dict__.items()
> if item[0][:2] != '__')
> name = subclass.__name__
> setattr(modfoo, name, type(name, (Wedge,), attrs))
>
> # Replace modfoo.Base with Wedge
> modfoo.Base = Wedge
That-is-neat! I'm impressed. It took me a while to understand it all:
I didn't know about __subclasses__, the way you fill the dictionary is
completely novel to me and the use of type(name, tuple, attributes)
was also completely novel to me. But eventually I got it and it's
quite neat. Thank you.
> Of course, there are plenty of ways this could break.
Uh-oh. Ok, you got me all excited and now I have to read the fine
prints. So, how can it break?
Ciao!
Manu
More information about the Python-list
mailing list