[Python-Dev] Shorthand for lambda
Hye-Shik Chang
hyeshik at gmail.com
Thu Mar 24 15:01:14 CET 2005
On Wed, 23 Mar 2005 10:33:53 -0600 (CST), Ka-Ping Yee
<python-dev at zesty.ca> wrote:
> Hey folks,
>
> >>> from placeholder import _
> >>> numbers = [5, 9, 56, 34, 1, 24, 37, 89]
> >>> filter(_ < 30, numbers)
> [5, 9, 1, 24]
> >>> map(_ + 10, numbers)
> [15, 19, 66, 44, 11, 34, 47, 99]
> >>>
>
> Look ma, no lambdas!
>
> I bet someone has already done this before, right?
>
I tried it once before:
ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/local-distfiles/perky/anonfunc-1.0.tar.gz
>>> from anonfunc import *
>>> f = (arg0 * arg1 + 3 * arg2) ** 2
>>> f(5, 6, 7)
2601
>>> f = kw1 and kw2
>>> f(kw1=False, kw2=True)
True
>>> f(kw2=False, kw1=True)
False
But there were some serious drawbacks to use it instead of lambda.
* (a,b,c) is impossible
* unable to use it with list comprehensions and generator expressions
* can't call functions and use its return value
* and many builtin functions as you described (such as len(), sorted())
Hye-Shik
More information about the Python-Dev
mailing list