a = b = 1 just syntactic sugar?

Steven Taschuk staschuk at telusplanet.net
Sat Jun 7 12:59:33 EDT 2003


Quoth Ed Avis:
> "Terry Reedy" <tjreedy at udel.edu> writes:
  [...]
> >The behavior is exactly as advertised: it turns an expression into a
> >function 'on the fly'.  Some people think this covers enough useful
> >cases to offset the nuisance of having it in the language.  Others
> >don't, and would like it removed in Python 3.
> 
> It's a pity that lambda is viewed as a nuisance rather than as an
> opportunity.  If it were made more general, and perhaps if the scoping
> rules were changed to permit closures, [...]

We already have closures, don't we?

    >>> def f(x):
    ...     def g(y):
    ...         return x + y
    ...     return g
    ... 
    >>> q = f(3)
    >>> q(4)
    7

Or do you want to be able to rebind names in outer scopes?

> [...] it could add a lot of power to
> the language (by which I mean permit some programs to be expressed
> more clearly and concisely).  The current implementation of lambda is
> a bit awkward, but that shouldn't speak against the idea of anonymous
> functions and anonymous code references.

Is anonymity really the point?  If so, it seems quite a minor one
to me -- it's just not that big a deal to have to name a small
function.  (Where are the complaints about having to name small
classes?)

Moreover, if the act of defining the function does not bind a name
to it, you need to get a reference to it some other way.  The
obvious thing to do is to have the function-defining text be an
expression which evaluates to the function object (so you stick it
in a list or whatever).  That in turn, as Python syntax is now,
entails that it can only contain expressions.

> [...] I can only note that many other
> languages support anonymous functions that can do all that a named
> function can, [...]

Sure; Lisp comes to mind.  But Lisp doesn't use indentation to
delimit blocks, nor does it distinguish between statements and
expressions, so the comparison is uninformative.

Do you have a language in mind which is a better analogue to
Python and could teach us more about these issues?

> [...] and that one possible way to implement lamvda in Python
> would be to turn a lambda-expression into a 'def' internally, with a
> freshly generated name.

That much is clear.  The main problem is syntactic; thus Terry's
suggestion that you produce a grammar rule for your proposal.
(That means BNF, not an informal description such as "anything
that can fit on a line".  Note that you might have to make
substantial changes to the rest of the grammar to obtain a
nonterminal which fits that informal description.)

  [...]
> What is strange is not so much the grammar rule for lambda itself, as
> the dividing line between what is a statement and what an expression.

Fwiw, I agree somewhat with this.  (It just doesn't bother me at
all in practice.)

  [...]
-- 
Steven Taschuk                  staschuk at telusplanet.net
"Telekinesis would be worth patenting."  -- James Gleick





More information about the Python-list mailing list