Python "is" behavior

michalis.avraam at gmail.com michalis.avraam at gmail.com
Fri Jun 20 12:45:25 EDT 2008


On Jun 20, 9:42 am, George Sakkis <george.sak... at gmail.com> wrote:
> On Jun 20, 12:31 pm, michalis.avr... at gmail.com wrote:
>
>
>
> > I am not certain why this is the case, but...
>
> > >>> a = 256
> > >>> b = 256
> > >>> a is b
>
> > True
>
> > >>> a = 257
> > >>> b = 257
> > >>> a is b
>
> > False
>
> > Can anyone explain this further? Why does it happen? 8-bit integer
> > differences?
>
> No, implementation-dependent optimization (caching). For all we know,
> the next python version may cache up to 1024 or it may turn off
> caching completely; do not rely on it. More generally, do not use 'is'
> when you really mean '=='.
>
> George

Thank you George. I am very curious about some of these internal
Python things that I keep stumbling upon through friends. And thank
you for all the help!



More information about the Python-list mailing list