a = b = 1 just syntactic sugar?

Kendear kendear at nospam.com
Tue Jun 3 09:44:35 CEST 2003


I don't like the fact that you can say in
Python

     a = b = 1

but you can't say

     a = (b = 1)

which is perfectly fine in C or Perl.

In those cases, "=" is just an operator
with associativity from right to left...
so adding parenthesis is not a problem.
But in Python, it would give an error.

 >>> a = b = 1
 >>> a = (b = 1)
SyntaxError: invalid syntax

I think if a language supports a = b = 1
then that means  "b = 1"  returns a value
of 1, which can be assigned to any variable.
But it seems like it is only a syntactic sugar
in Python?

if Python supports   1 < a < 10
then maybe it is also just syntactic sugar.
Other language might take it as  (1 < a) < 10
which is just the boolean 0 or 1 less than 10
which is always true.

 >>> a = -1
 >>> 1 < a < 10
0
 >>> (1 < a) < 10
1

so in Python, we can't just add parenthesis
to multiple =  and can't add parenthesis
arbitrarily to multiple  <, >, ==, etc.







More information about the Python-list mailing list