Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Tue Jul 30 07:29:25 EDT 2002


"Huaiyu Zhu" <huaiyu at gauss.almadan.ibm.com> wrote in message
news:slrnakb792.aej.huaiyu at gauss.almadan.ibm.com...
> 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.

Exactly. AFIK, everyone has been pursuing the first path,
while it is conceptually simple to do the second. See my response to
Steve Holden's comment for how it would look in practice.
It's easy to write, not quite so easy to look at. I have no idea whether
it would be easy to implement in the compiler. (The interpreter
shouldn't
care.)

The trick is to use 'def' as the keyword, not 'lambda.'

>
> 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.

Why bother with the restrictions? Fredrik had the right idea when
he said that we needed something to make it obvious that functions
are first class objects. That is, allow anonamous functions anywhere
you can have a function object.

John Roth
>
> Huaiyu





More information about the Python-list mailing list