En Tue, 07 Apr 2009 20:45:15 -0300, Grant Edwards <gra...@visi.com> escribió:
On 2009-04-07, Scott David Daniels <scott.dani...@acm.org> wrote:
Grant Edwards wrote:
On 2009-04-07, Scott David Daniels <scott.dani...@acm.org> wrote:

File "/usr/lib/python2.5/site-packages/ClientForm.py", line 2016, in add_to_form
       Control.add_to_form(self, form)
TypeError: unbound method add_to_form() must be called with FancyControl instance as first argument (got CheckboxControl instance instead)

The monkey-patching only happens after the ClientForm module
has been executed (first import), and the monkey-patching
happens after all of that.  So now the "tack it into the
class" method looks a bit better if you cannot simply add your
requirement to the ClientForm source.

That's obviously the "right thing", but it makes portability
more of an issue (I would have to archive/distribute ClientForm
source and users would have to install the customized version
of ClientForm).

Of course there's always the chance that my version of
monkey-patching will stop working with a different version of
ClientForm.  We'll burn that bridge when we come to it.

What you might use as a half-way measure:

     class Mixin:  # or class Mixin(object) if new-style:
         def __eq__(self, other):
             return (self.type == other.type ...
         def __ne__(self, other):
             return not self.__eq__(other)
     class FancyControl(MixIn, ClientForm.Control): pass
     class FancyCheckboxControl(MixIn, ClientForm.CheckboxControl): pass
     ..
     ClientForm.Control = FancyControl
     ClientForm.CheckboxControl = FancyCheckboxControl

That would work -- but there are probably 8 or 10 different
Control subclasses. It's a bit tedious mixing them all one at a
time, and you need more "inside" information (the names of all
the different subclasses).

New style classes have a __subclasses__() method that could be used to find all of them (*at a certain moment*) -- but considering all the issues, I think that monkey-patching the base class is the "less bad" option in this case...

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to