Steven D'Aprano steve at
Sat Jun 2 03:12:34 CEST 2007

On Fri, 01 Jun 2007 07:09:50 -0400, Steve Holden wrote:

>>>>> The real answer is of course: Use a function.
>>>> But what about something like
>>>> lambda x: sin(y)+cos(y) where y=x*x
>>>> ?
>>>> May be this could be a PEP? If there is no straight way to do this.
>>> def f(x):
>>>    y = x*x
>>>    return sin(y) + cos(y)
>>> What is not straightforward about that?
>> This code is needed once in a map, so I don't want 3+ extra lines.
>> Solution seemed so simple...
>> I always considered python as languague, where simple things do not require 
>> extensive coding.
>> Moreover, this construction is common thing in functional programming. 
> Stop thinking of three lines as "extensive coding" and your problem 
> disappears immediately.

The F-bot once suggested adding a clause to the Zen of Python about
"writing two lines of code is not a sin" or "cramming two lines of code
into one is not a virtue" (my paraphrases). 

Check the two alternatives:

def f(x):
    y = x*x
    return sin(y) + cos(y)

44 key presses, including tabs and newlines and a blank line after the
function, but excluding counting the shift key separately.

lambda x: (lambda y: sin(y) + cos(y))(x*x)

42 key presses.

Apart from the extremely minor issue of "namespace pollution", I think
that speaks for itself.


More information about the Python-list mailing list