a = b = 1 just syntactic sugar?

Asun Friere afriere at yahoo.co.uk
Wed Jun 4 22:32:54 EDT 2003


Peter Hansen <peter at engcorp.com> wrote in message news:<3EDD6610.3CABE86A at engcorp.com>...

> 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

I had assumed that it was /because/ of the small integer cache that in
the example a = b = 2, a and b evaluated true for identity.  What I
did /not/ realise is that assigning large integers in the same
statement, or apparently even in several statements on the same line
would have the same effect.

? a = 556; b = 556
? a is b
1




More information about the Python-list mailing list