Replacing methods without subclassing

xtian xtian at toysinabag.com
Mon Jan 27 00:19:33 EST 2003


Aum <spam-me at no-thanks.com> wrote in message news:<2_MY9.47989$F63.1032275 at news.xtra.co.nz>...
> Thanks all the same, but some kind soul on OPN #python told me a much 
> simpler way, which I post for benefit of others looking for similar.
> 
> 
> import new
> ...
> 
> def setevhandler(self, func):
>         """
>         Assigns a new event handler function to the class (or subclass)
>         instance. This provides an alternative to subclassing.
>         """
>         cls = self.__class__
>         if type(func) is not types.FunctionType:
>                 raise MyNotFunctionError
>         self.handleevent = new.instancemethod(func, self, cls)
> 

That is simpler, but type-checking (the "if type(func)" line) is
almost always a bad idea, preventing potentially useful polymorphism.
In this case you'll be stopping people from passing a class with a
__call__ magic method, or a bound method from another instance. And
although I can't think of a good reason someone might want to do that,
I'm sure that the clever people here can. :)

A way of doing the same thing, which follows the Python idiom of
"checking the interface, rather than the type," would be to use the
callable() builtin.

 def setevhandler(self, func):
         """
         Assigns a new event handler function to the class (or
subclass)
         instance. This provides an alternative to subclassing.
         """
         cls = self.__class__
         if not callable(func):
                 raise CallableRequiredError
         self.handleevent = new.instancemethod(func, self, cls)


(Another tip you might find useful is being able to specify the
handler's name (if you want to attach multiple handlers to one
instance). This would be done by adding a name parameter, and then
changing the last line of the function to:

  setattr(self, name, new.instancemethod(func, self, cls))

Sorry if you already know this!)

Cheers,
xtian




More information about the Python-list mailing list