[Python-ideas] Thoughts on lambda expressions

Grant Jenks grant.jenks at gmail.com
Wed Mar 2 18:14:02 EST 2016


On Wed, Mar 2, 2016 at 3:02 PM, Ed Minnix <egregius313 at gmail.com> wrote:

> Hi, I would just like to add a side note on this:
>
> The library fn.py implements a class called _Callable, which gives a
> shorter notation for lambdas, using an underscore ( _ ), instead of
> declaring parameters (for example, map(_ + 1, range(10)) is the same as
> map(lambda n: n + 1, range(10)) ). In addition to the regular arithmetic
> operators, it supports the __getattr__ method. (for instance _.y is the
> same as lambda a: a.y)
>
> Therefore, you do not override the general syntax of Python (other than
> the fact that you cannot use the code
>
> for i, _ in some_iterable:               # don’t use, at the end of the
> loop, fn._ has disappeared from the scope of the module
>         do_something(i)
>
> )
>
> Personally, I would propose the adoption of the _ in some standard library
> module (e.g., functools) rather than overriding the “from” syntax if the
> simplification of lambdas in a goal. (Personal I find the _ much more
> user-friendly)
>
> - Ed M


+1 for fn.py's underscore functionality.

But underscore already has a purpose as a placeholder and in the repl so I
think it's a poor choice. In Python 3, you can actually do:

from fn import _ as λ
print(sorted(cards, key=λ.suit))

But that is hard to type on most keyboards. What if we allowed question
marks in identifiers and used ``?.suit`` ?

Grant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160302/36a9091d/attachment.html>


More information about the Python-ideas mailing list