A Summary: Expression-Assignments. (Very Long)

Fredrik Lundh fredrik at pythonware.com
Wed May 12 10:16:48 EDT 1999


> Actually, I find this part of Python a bit inconsistent since:
> 
> a=b=1 is valid while
> a=(b=1) is not...
> 
> To me the fact that 'a=b=1' works implies that the 'b=1' bit is
> actually returning a value.

it's not fair to read Python code using the grammar
for another language...

here's the relevant part of the Python grammar:

    http://www.python.org/doc/current/ref/assignment.html

(you'll find the full grammar in the Grammar/Grammar file
in the source distribution)

the interesting part is:

    assignment_stmt: (target_list "=")+ expression_list

or in other words, an assignment STATEMENT consists
of one or more target lists, each followed by "=", and
finally exactly one expression list.  there is no assign-
ment expression.

in C, things look more like:

    assignment_expr:
        conditional_expr |
        unary_expr assignment_op assignment_expr

or in other words, an assignment EXPRESSION consists
of either a conditional expression, or a unary expression
followed by an assignment OPERATOR (=, +=, %=, etc),
followed by another assignment expression.

> This should perhaps be more consistent in Python by
> either making 'a=b=1' illegal, or making 'a=(b=1)' legal.

by some odd reason, Python uses a Python grammar.
not a C, C++, or Java grammar.  strange but true ;-)

</F>





More information about the Python-list mailing list