Terry Hancock wrote:

> On Friday 01 July 2005 03:36 pm, Ron Adam wrote:
> 
>>I find map too limiting, so won't miss it.  I'm +0 on removing lambda 
>>only because I'm unsure that there's always a better alternative.
> 
> 
> Seems like some new idioms would have to be coined, like:
> 
> def my_function(a1, a2):
>     def _(a,b): return a+b
>     call_a_lib_w_callback(callback=_)
> 
> doesn't seem too bad, and defeats the "wasting time thinking of names"
> argument.

Usually the time is regained later when you need to go back and figure 
out what it is that the lambda is doing.  Not so easy for beginners.

A hard to understand process that is easy to do, is not easier than an 
easy to understand process that is a bit harder to do.


>>So what would be a good example of a lambda that couldn't be replaced?
>  
> I suspect the hardest would be building a list of functions. Something
> like:
> 
> powers = [lambda a, i=i: a**i for i in range(10)]
> 
> which you might be able to make like this:
> 
> powers = []
> for i in range(10):
>     def _(a,i=i): return  a**i
>     powers.append(_)
> 
> which works and is understandable, but a bit less concise.

This would be a more direct translation I think:

     def put_func(i):
         def power_of_i(a):
            return a**i
         return power_of_i
     power = [put_func(i) for i in range(10)]

I think it's also clearer what it does.  I had to type the lambda 
version into the shell to be sure I understood it.  I think that's what 
we want to avoid.

> The main obstacle to the lambda style here is that def statements
> are not expressions.  I think that's been proposed as an alternative,
> too -- make def return a value so you could say:
> 
> powers = [def _(a,i=i): return a**i for i in range(10)]

Wouldn't it be:

    powers = [(def _(a): return a**i) for i in range(10)]

The parens around the def make it clearer I think.

That would be pretty much just renaming lambda and changing a syntax a 
tad.  I get the feeling that the continual desire to change the syntax 
of lambda is one of the reasons for getting rid of it.  I'm not sure any 
of the suggestions will fix that.  Although I prefer this version over 
the current lambda.

Instead of reusing 'def', resurrecting 'let' as a keyword might be an 
option.

    powers = [ (let a return a**i) for i in range(10) ]


I just now thought this up, but I like it a lot as an alternative syntax 
to lambda.  :-)


> Personally, I think this is understandable, and given that lambda
> is to be pulled, a nice substitute (I would say it is easier to read
> than the current lambda syntax, and easier for a newbie to
> understand).
> 
> But it would probably encourage some bad habits, such as:
> 
> myfunc = def _(a,b):
>     print a,b
>     return a+b
> 
> which looks too much like Javascript, to me, where there are
> about three different common idioms for defining a
> function (IIRC).  :-/

I don't think it would be used that way...  Very often anyway.

Still none of these examples make for good use cases for keeping lambda. 
  And I think that's whats needed first, then the new syntax can be decided.

Ron
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to