Let's Talk About Lambda Functions!

Bengt Richter bokr at oz.net
Thu Aug 1 21:15:39 CEST 2002

On Mon, 29 Jul 2002 19:53:07 +0000 (UTC), huaiyu at gauss.almadan.ibm.com (Huaiyu Zhu) wrote:

>Fredrik Lundh <fredrik at pythonware.com> wrote:
>>to fix the other "mistake", some needs to come up with a
>>really great syntax (and get michael hudson to implement
>>it for them ;-).  ideas are welcome.
>No great syntax here, but some ideas on why it is difficult syntax-wise (in
>the hope that someone can overcome them).
>Python uses indentation to denote lexical scope at or above the statement
>level, while using parenthesis at the expression level.  It is rather easy
>to embed parenthesis inside indentation, but the other way is difficult.
>So to allow statements in lambdas, it is either necessary to delimit
>statements with some begin/end marker in lieu of indentation, or to come up
>with a way to embed indentation scoping inside parenthesis scoping.
>Another possibility is to allow only those statements that do not have
>further scopes.  For example, print and assert would be allowed but
>if/then/else would not.
I think it becomes simple if you enclose the anonymous def expression itself in parentheses.
Then the def can start at any indent, and its suite can be parsed relative to that
as usual. The closing paren makes it easy to continue the expression wherever on
the line you want (so long as the final dedent is triggered) following it with (args)
or a comma if building a list or tuple, etc. See other post for example.

Bengt Richter

More information about the Python-list mailing list