[Python-Dev] any support for a methodcaller HOF?

Bengt Richter bokr at oz.net
Sun Feb 5 19:45:52 CET 2006


On Sat, 4 Feb 2006 20:17:15 +0100, Eric Nieuwland <eric.nieuwland at xs4all.nl> wrote:

>Nick Coghlan wrote:
>>> I believe that usage of a keyword with the name of a Greek letter also
>>> contributes to people considering something broken.
>>
>> Aye, I agree there are serious problems with the current syntax. All 
>> I'm
>> trying to say above is that I don't believe the functionality itself 
>> is broken.
>
>Lambda is not broken, it's restricted to  single calculation and 
>therefore of limited use.
It's not even that restricted, if you want to be perverse, e.g.,

 >>> (lambda w:eval(compile("""if 1: # indented looks nicer ;-)
 ...     if len(w)<=3: adj ='short'
 ...     elif len(w)<=5: adj ='medium length'
 ...     else: adj = 'long'
 ...     print 'Hi, %s! I would say you have a %s name ;-)'%(w,adj)
 ... """,'','exec')))('Monty')
 Hi, Monty! I would say you have a medium length name ;-)

lazy copy/pasting and changing the arg:

 >>> (lambda w:eval(compile("""if 1: # indented looks nicer ;-)
 ...     if len(w)<=3: adj ='short'
 ...     elif len(w)<=5: adj ='medium length'
 ...     else: adj = 'long'
 ...     print 'Hi, %s! I would say you have a %s name ;-)'%(w,adj)
 ... """,'','exec')))('Ada')
 Hi, Ada! I would say you have a short name ;-)

My point is that ISTM preventing easy inclusion of suites in lambda/anonymous_def
is more of a morality/taste/catechistic issue than a technical one. It seems like
an attempt to control coding style by disincentivizing the disapproved.
That may be ok in the big picture, I'm not sure, but IMO transparency of motivations is best.

>Although I wasn't too serious (should had added more signs of that), an 
>anonymous 'def' would allow to use the full power of method definition.
>
It's already allowed, just not in a way that generates efficient code (although
the above can be improved upon, let's not go there ;-)

>> At last count, Guido's stated preference was to ditch the functionality
>> entirely for Py3k, so unless he says something to indicate he's 
>> changed his
>> mind, we'll simply need to continue with proposing functions like
>> methodcaller() as workarounds for its absence...
>
>Yep, we'll just have to learn to live without it. :-( / ;-)
If it's needed, I believe a way will be found to have it ;-)
I do think the current lambda serves a valuable purpose, so I
hope some way is found to preserve the functionality, whatever
problems anyone may have with its current simple syntax.
Psst, Nick, how about
    (x*y for x,y in ()) ? # "()" as mnemonic for call args
;-)

Regards,
Bengt Richter



More information about the Python-Dev mailing list