A critic of Guido's blog on Python's lambda

Ken Tilton kentilton at gmail.com
Sat May 6 01:30:04 EDT 2006



Steve R. Hastings wrote:
> On Fri, 05 May 2006 21:16:50 -0400, Ken Tilton wrote:
> 
>>The upshot of 
>>what he wrote is that it would be really hard to make semantically 
>>meaningful indentation work with lambda.
> 
> 
> Pretty much correct.  The complete thought was that it would be painful
> all out of proportion to the benefit.
> 
> See, you don't need multi-line lambda, because you can do this:
> 
> 
> def make_adder(x):
>     def adder_func(y):
>         sum = x + y
>         return sum
>     return adder_func
> 
> add5 = make_adder(5)
> add7 = make_adder(7)
> 
> print add5(1)  # prints 6
> print add5(10)  # prints 15
> print add7(1)  # prints 8
> 
> 
> Note that make_adder() doesn't use lambda, and yet it makes a custom
> function with more than one line.  Indented, even.
> 
> You could also do this:
> 
> 
> lst = []  # create empty list
> def f(x):
>     return x + 5
> lst.append(f)
> del(f)  # now that the function ref is in the list, clean up temp name
> 
> print lst[0](1)  # prints 6
> 
> 
> Is this as convenient as the lambda case?
> 
> lst.append(lambda x: x + 7)
> print lst[1](1)  # prints 8
> 
> 
> No; lambda is a bit more convenient.  But this doesn't seem like a very
> big issue worth a flame war.

<g> Hopefully it can be a big issue and still not justify a flame war.

Mileages will always vary, but one reason for lambda is precisely not to 
have to stop, go make a new function for this one very specific use, 
come back and use it as the one lambda statement, or in C have an 
address to pass. but, hey, what are editors for? :)

the bigger issue is the ability of a lambda to close over arbitrary 
lexically visible variables. this is something the separate function 
cannot see, so one has to have a function parameter for everything.

but is such lexical scoping even on the table when Ptyhon's lambda comes 
up for periodic review?

>  If GvR says multi-line lambda would make
> the lexer more complicated and he doesn't think it's worth all the effort,
> I don't see any need to argue about it.

Oh, no, this is just front porch rocking chair BS. But as an enthuiastic 
developer I am sensitive to how design choices express themselves in 
ways unanticipated. Did the neat idea of indentation-sensitivity doom 
pythonistas to a life without the sour grapes of lambda?

If so, Xah's critique missed that issue and was unfair to GvR in 
ascribing his resistance to multi-statement lamda to mere BDFLism.

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.



More information about the Python-list mailing list