Securing a future for anonymous functions in Python
Simo Melenius
firstname.lastname at iki.fi-spam
Fri Dec 31 16:56:09 EST 2004
Ian Bicking <ianb at colorstudy.com> writes:
> But I do think there's other ways to approach this. Function
> expressions could get really out of hand, IMHO, and could easily lead
> to twenty-line "expressions". That's aesthetically incompatible with
> Python source, IMHO.
You can already write unaesthetic hundred-line Python functions, if
you want to. Python's syntax doesn't yet impose a restriction on the
number of sequential statements :-) It sounds artificial to impose
such restrictions on these hypothetical "inline blocks", even if by
only allowing them to be plain expressions.
IMHO, the most pythonic way to write an "inline-block" is by reusing
existing keywords, using Python-like start-of-blocks and ending it by
indentation rules:
map (def x:
if foo (x):
return baz_1 (x)
elif bar (x):
return baz_2 (x)
else:
global hab
hab.append (x)
return baz_3 (hab),
[1,2,3,4,5,6])
and for one-liners:
map (def x: return x**2, [1,2,3,4,5,6])
As a side-effect, we also
- got rid of the "lambda" keyword;
- strenghtened the semantics of "def": a "def" already defines a
function so it's only logical to use it to define anonymous
functions, too;
- unified the semantics: function is always a function, and functions
return values by using "return". When learning Python, I learned the
hard way that "lambda"s are expressions, not functions. I'd pay the
burden of writing "return" more often in exchange for better
consistency.
my two cents,
br,
S
More information about the Python-list
mailing list