Extending a class on runtime

rdaunoravicius at gmail.com rdaunoravicius at gmail.com
Tue Mar 27 21:05:54 CEST 2007


Hi,

Let's say you have a bunch of instatiated objects of the same class on
your hands and you want to had some functionality to them.

I'm facing this situation while working with PyGTK and libglade to
create a GUI. Libglade creates a whole object tree representing the
GUI out of an XML file, and a bunch of GtkComboBox objects are
instantiated. I don't like the way GtkComboBox objects works, so I'd
like them to have some extra methods. Inheriting and extending
GtkComboBox is pointless because I'm not the one instantiating the
class. I only came up with three possibilities:

    A) Adding my methods to the objects in a for-loop

    B) Adding my methods to the GtkComboBox class (I tried this and it
seems to work)

    C) Create a GtkComboBoxExtended class inheriting from GtkComboBox
and change the instances' class in a for-loop.

I'm kind of inclined to C. B sounds dangerous and A is plain ugly.
I'm very new to this and I'm sure there is a well-established pythonic
way to solve this problem, so I'm appealing for  your vast collective
wisdom to point me in the path of righteousness.

Thanks,
Rodrigo




More information about the Python-list mailing list