[Python-ideas] Thoughts on lambda expressions

Abe Dillon abedillon at gmail.com
Wed Mar 2 19:19:42 EST 2016


On Wednesday, March 2, 2016 at 4:45:22 PM UTC-6, Ethan Furman wrote:
>
> what does it look like when you have more than one paramater in the 
> signature and/or something beside simple attribute lookup? 


The signature follows the exact same rules as a lambda signature:

(func(*args, *kwargs) + offset from func, *args, offset, **kwargs)

# vs.


lambda func, *args, offset, **kwargs: func(*args, **kwargs) + offset

Those both look bad because the expressiveness of lambdas loses its charm 
when the signature gets long and complicated.

   'open': lambda s, cr, uid, rec, ctx: rec['state'] == 'draft', 
>
> would instead be: 
>
>    'open': rec['state'] == 'draft' from (s, cr, uid, rec, ctx), 
>
> Ouch.  That just went from bad to horrid. 
>

Not only did you get the syntax wrong, but the lambda version is also 
horrid which is exacerbated by the fact that it looks like your trying to 
give the function a name or otherwise store it which defeats whole purpose 
of a lambda. At that point just use 'def' and stop trying to use lambda 
where it is ill suited.

 

>
> > b) It doesn't use the esoteric name, 'lambda' which causes its own 
> > readability issues. 
>
> On the contrary:  'lambda' lets you know immediately what you're dealing 
> with.  The syntax you are suggesting looks like: 
>
> - a (wrong) generator 
> - taking ... items? ... from some kind of container 
>

the idea is from the recipe metaphor for a function:

def <recipe>(<ingredients>):
    ...instructions to cook ingredients

vs.

 (<make_something> from <ingredients>)

It's fair to quibble over the exact implementation (maybe use 'with' 
instead of 'from') but the main point of the syntax it to put the important 
bit (i.e.the expression) in front of the (usually) unimportant bit (i.e. 
the signature) and to swap out an esoteric word (lambda) with something 
that continues the readability emphasis of Python by using more common 
words.
 

>
> To be fair, it looked like an interesting syntax at first glance, but 
> deeper investigation shows serious drawbacks. 
>
> -- 
> ~Ethan~ 
> _______________________________________________ 
> Python-ideas mailing list 
> Python... at python.org <javascript:> 
> https://mail.python.org/mailman/listinfo/python-ideas 
> Code of Conduct: http://python.org/psf/codeofconduct/ 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160302/8e42f0cb/attachment.html>


More information about the Python-ideas mailing list