Securing a future for anonymous functions in Python
ncoghlan at iinet.net.au
Thu Dec 30 15:19:29 CET 2004
Jp Calderone wrote:
> On Fri, 31 Dec 2004 00:00:31 +1000, Nick Coghlan <ncoghlan at iinet.net.au> wrote:
>>Paul Rubin wrote:
>>>Nick Coghlan <ncoghlan at iinet.net.au> writes:
>>>>Anyway, I'm looking for feedback on a def-based syntax that came up in
>>>>a recent c.l.p discussion:
>>>Looks like just an even more contorted version of lambda. It doesn't
>>>fix lambda's main deficiency which is inability to have several
>>>statements in the anonymous function.
>>Do you consider generator expressions or list comprehensions deficient because
>>they don't allow several statements in the body of the for loop?
> >>> (foo + bar
> ... for foo in range(13) if foo % 3 == 2
> ... for bar in range(16, 23) if 2 <= bar % 5 < 4)
> <generator object at 0xb7dec10c>
> Hmm. Two for loops and two if clauses. That's four altogether.
> Does that qualify as "several"? :) Sure, they're not statements
> according to the grammar, but that's entirely beside the point.
And that's an expression, and hence perfectly legal as the body of an anonymous
So if we feel like abusing our anonymous functions, we use generator expressions
to get if statements and for loops, sys.stdout.write() for print statements,
sys.stdin.read() for input statements, anonymous functions for def statements,
and throw in our own functions with lazily evaluated arguments for anything that
isn't already covered by the interpreter core (which isn't much).
I just don't understand why people complain so much about the restriction to a
single expression in lambdas, yet there is nary a peep about the same
restriction for generator expressions and list comprehensions.
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
More information about the Python-list