a = b = 1 just syntactic sugar?

Peter Hansen peter at engcorp.com
Tue Jun 3 23:22:56 EDT 2003


Asun Friere wrote:
> 
> "Batista, Facundo" <FBatista at uniFON.com.ar> wrote in message news:<mailman.1054662726.12651.python-list at python.org>...
> 
> > "b = 1" does not return 1, just returns b. a and b are the same object now
> > (they are not just equal, they are the very same).
> >
> They are, but this has little to do with the fact that both assigned
> their values using a single statement. ie:
> >>>sys.ps1 = '? '
> ? a = 1
> ? b = 1
> ? a is b
> 1

Actually it has _everything_ to do with the mechanism by which your 
example would produce a result of 1 except with low integers.

The interpreter pre-constructs and caches all the integers from -1 (?)
to 100 (or maybe it's from 0 to 100... whatever) and always reuses them
instead of creating new objects with the same integer values.  Try this:

>>> a = 556
>>> b = 556
>>> a is b
0
>>> a = b = 556
>>> a is b
1

-Peter




More information about the Python-list mailing list