4 ms·
I'm a huge fan of the lambda syntax : x -> x * 2 It looks much cleaner than the traditional : lambda x : x * 2 Would a PEP adding this syntax have any chanc
by Majestic121 8y ago
I'm a huge fan of the lambda syntax :
x -> x * 2
It looks much cleaner than the traditional :
lambda x : x * 2
Would a PEP adding this syntax have any chance to pass ?
- orblivion 8y agoI think Guido wanted to get rid of lambdas altogether one time. If anything I would guess they want to keep it ugly to discourage its use.
- leephillips 8y agoThen you should take a look a Julia, if you haven't already.
- Waterluvian 8y agoI've heard the argument that you ought to consider writing a named function and using it where the lambda would have gone. Self documents and cuts down on lines of symbol soup. Python wants to be broadly good enough for everyone, not perfect for any one person's specific domain and expertise level. I think this might be the right attitude.
- runarberg 8y agoJust came back to python after several years in javascript. I've found that the functions you need are often already provided in the standard library (for example the `operator` module[1]). So where in javascript you would write: const double = x => x * 2 In python you can simply write: from operator import mul double = mul(2) [1]: https://docs.python.org/2/library/operator.html https://docs.python.org/2/library/operator.html
- Franciscouzo 8y agoThat won't work, you have to use something like partial instead if you want to use the operator module: from functools import partial import operator double = partial(operator.mul, 2) And at that point I think it would way simpler to just define a normal function.
- bakery2k 8y agoThe Python equivalent of your JavaScript example is simply: def double(x): return x * 2 Python's `lambda` construct creates anonymous functions - its limitations mean it should only be used when anonymity is required. If a function is to be given a name, there is no reason to use a lambda over standard function syntax.
- mFixman 8y agoThat don't work since you need to use `partial` on `mul`. I'm a big fan of the way Haskell manages operators and partial application. double :: Num a => a -> a double = (2 *)
- psychometry 8y agoThis is basically what R offers: both -> and <- assignment operators. The addition of a piping operator in Coconut is great, too, for people familiar with dplyr.
- h8hawk 8y agoI don't think so, Guido has a long time history of being against FP, and the core of language is statement oriented rather than expression oriented. Beside that "->" operator in python is already used as return type annotation for functions. For the record Julia use exactly this syntax for lambdas. Like: foo = a -> b -> a+2*b It's pretty lispy language.
- bakery2k 8y agoWould it be possible for Python's parser to handle the infix `->` syntax, without making it more complex than the current LL(1)?