Extending a class on runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rdaunoravicius@gmail.com

    #1

    Extending a class on runtime

    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 GtkComboBoxExte nded 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

  • Bruno Desthuilliers

    #2
    Re: Extending a class on runtime

    rdaunoravicius@ gmail.com a écrit :
    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.
    Then I'd just do it.
    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)
    I don't have much experience with PyGTK, but unless GtkComboBox is a
    very special class object, it should JustWork(tm).
    C) Create a GtkComboBoxExte nded class inheriting from GtkComboBox
    and change the instances' class in a for-loop.
    That's another possible solution, but it requires more work.
    I'm kind of inclined to C. B sounds dangerous
    Why ? Eventually confusing if not well documented, but mostly harmless
    IMHO. Python is dynamic by nature, and there's no reason to not take
    advantage of this fact.
    and A is plain ugly.
    It's also the more complicated and less efficient solution IMHO (unless
    you want different implementations of the same method on a per-instance
    basis...).
    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.
    As far as I'm concerned, and if it's just a matter of adding a couple of
    methods, I'd go for B, which is the SimplestThingTo Do(tm).

    Now if it's a big complicated project with a rather unstable team, C is
    perhaps a bit more explicit since it makes obvious that this not a
    pristine GtkComboBox no more. But even then, it's always possible to
    monkeypatch a class in a more or less documented way (explict mentions
    of the patch in added/replaced/extended methods and in __repr__()).

    My 2 cents...

    Comment

    Working...