a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Sun Jun 8 06:20:30 EDT 2003


martin at v.loewis.de (Martin v. =?iso-8859-15?q?L=F6wis?=) writes:

>>>What is the output of
>>>
>>> print a,lambda:print b,c
>> 
>>As I mentioned elsewhere in this thread, the lambda definition could
>>be parenthesized where the parse would otherwise be ambiguous.

>That doesn't answer my question. What would be the output of this
>statement?

It is syntactically ambiguous because of the two print statements both
accepting commas.  You'd need to write

    print a, (lambda: print b, c)

which prints the value of a followed by '<function <lambda> ...>', or

    print a, (lambda: print b), c

which prints the value of a, a string representing the function, and
then the value of c.

>>Is it a rule of Python's grammar that no construct should ever require
>>parenthesizing just to stop it being ambiguous?  
>
>No, there is no such rule.

So there is no reason to reject out of hand allowing one-line
statements in lambda just because such constructs may require
parenthesizing when used inside larger statements like 'print'.  There
may be other grammatical reasons of course.

>The 'problem', at the moment, is that you fail to specify what
>exactly the grammar is that you are proposing.

Elsewhere in this thread I have mentioned

    lambda_form ::=
                 "lambda" [parameter_list]: simple_stmt

which seems to match the specification of 'any statement that can fit
on one line' (which itself seems pretty exact to me).

>Then, if you have completed the specification, the problem likely is
>that your proposed grammar is either counter-intuitive, or
>unimplementable (depending on your specification).

Let's see which it is.

-- 
Ed Avis <ed at membled.com>




More information about the Python-list mailing list