[Tutor] Need some clarification on this

xDog Walker thudfoo at gmail.com
Sat Mar 19 16:47:48 CET 2011


On Saturday 2011 March 19 08:35, Emmanuel Ruellan wrote:
> 2011/3/19 Yaşar Arabacı <yasar11732 at gmail.com>
>
> > >>>a=5
> > >>>b=5
> > >>>a == b
> >
> > True
> >
> > >>>a is b
> >
> > True
> >
> > My question is, why "a is b" is true. What I expected it to be is that, a
> > and b are different things with same value.
>
> Even stranger:
> >>> a = 10**10
> >>> b = 10**10
> >>> a == b
>
> True
>
> >>> a is b
>
> False
>
> >>> a = 5
> >>> b = 5
> >>> a == b
>
> True
>
> >>> a is b
>
> True
>
> In the general case, you're right: a and b point to two different objects,
> but there is also some kind of optimisation for small numbers, and as a
> result when a = 5 and b = 5, both point the same '5' object.
>
> Emmanuel Ruellan

From http://docs.python.org/c-api/int.html

    The current implementation keeps an array of integer objects for all 
integers between -5 and 256, when you create an int in that range you 
actually just get back a reference to the existing object.
-- 
I have seen the future and I am not in it.


More information about the Tutor mailing list