Binding first parameter of method to constant value

Johannes Bauer dfnsonfsduifb at gmx.de
Fri May 18 09:28:46 EDT 2012


On 18.05.2012 15:10, Steven D'Aprano wrote:

> Here's one way:
> 
> import types
> class K(object):
>     def _dispatcher(self, name, *args):
>         print "called from", name, args
>     def __init__(self):
>         setattr(self, 'foometh',
>                 types.MethodType(
>                 lambda self, *args: self._dispatcher('foometh', *args),
>                 self, self.__class__)
>                 )
>         setattr(self, 'barmeth',
>                 types.MethodType(
>                 lambda self, *args: self._dispatcher('barmeth', *args),
>                 self, self.__class__)
>                 )

Aaaah, amazingly beautiful! Thank you very much.

Didn't know that lambda functions could have a variable number of
arguments in Python (never tried that before). Learned something about
Python again. No words to describe how much I love the language :-))

Best regards,
Joe

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>



More information about the Python-list mailing list