a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Sun Jun 8 07:12:33 EDT 2003


"Terry Reedy" <tjreedy at udel.edu> writes:

>>I am just asking, why is it that the body of an anonymous function
>>cannot contain statements?
> 
>Since you have rejected my and others explanation, I will stop.

So far people have mentioned that Python distinguishes between
expressions and statements, which is a description of 'what' the
language does, but they haven't really explained 'why' this means that
an anonymous function definition, which is an expression, could not
contain a one-line statement.

It seems a little like this conversation with a vegetarian:

    - Will you have some bacon?
    - No thank you.
    - Why not?
    - I don't eat meat.
    - Why don't you eat meat?
    - I have already given you my explanation.

>>The fact that the expression / statement distinction exists is not
>>in itself a reason to prohibit statements from appearing in
>>anonymous functions.
> 
>You have yet to demonstrate that it is sensibly possible to do so.

I've given several examples, a possible grammar rule (use simple_stmt)
and a semantics (the same as a named def).

>>It seems to me from the Python grammar that the production
>>simple_stmt is a statement that fits on one line.
> 
>You clipped and did not answer my question as to what you mean by
>'line'.

What I meant by my answer was that for a formal definition of the idea
'statement that fits on one line', I propose to use simple_stmt from
the grammar.  I hope this is precise enough.

Instinctively I thought that a statement X fits on a line if

    def f():
      X

is a legal Python program, where X is a complete statement (that is,
it doesn't have unclosed strings or parentheses, and matches the
'statement' production in the grammar) and X does not contain a
newline character or ';' (which to me seems like a different way of
writing a newline).

But a more formal way of expressing this is to use the simple_stmt
production.

>You clipped and did not answer my question about multiple lambdas on
>one line.

I mentioned earlier that sometimes parentheses would be needed to
indicate the right way to group things (but I think my proposal does
not require any existing valid programs to be parenthesized).  By
multiple lambdas on one line did you mean something like:

    (f, g) = ((lambda x: print x), (lambda y: y + 5))

-- 
Ed Avis <ed at membled.com>




More information about the Python-list mailing list