a = b = 1 just syntactic sugar?

Steven Taschuk staschuk at telusplanet.net
Tue Jun 3 12:18:05 EDT 2003


Quoth Kendear:
  [...]
> 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?

Right.  Note also that the assignments happen left to right; try

    d = {}
    i = 0
    i = d[i] = 4
    print d

(Though obviously it's not good style to exploit this detail in
real code.)

The relevant part of the Language Reference is
    <http://www.python.org/doc/current/ref/assignment.html>

> if Python supports   1 < a < 10
> then maybe it is also just syntactic sugar.

Right.  It's the same as
    1 < a and a < 10
except that the middle expression is evaluated only once.  See
    <http://www.python.org/doc/current/ref/comparisons.html>

> Other language might take it as  (1 < a) < 10
> which is just the boolean 0 or 1 less than 10
> which is always true.

... which is one reason Python is better than such languages.  The
test 1 < a < 10 does in Python is actually useful, and in keeping
with the mathematical notation.

-- 
Steven Taschuk                                7\ 7'Z {&~         .
staschuk at telusplanet.net                        Y r          --/hG-
                                            (__/ )_             1^1`





More information about the Python-list mailing list