Strange behaviour of 'is'

Ben C spamspam at spam.eggs
Thu Sep 21 16:37:14 EDT 2006


On 2006-09-21, Fijoy George <tofijoy at yahoo.co.in> wrote:
> Hi all,
>
> I am a bit perplexed by the following behaviour of the 'is' comparator
>
>>>> x = 2.
>>>> x is 2.
> False
>>>> y = [2., 2.]
>>>> y[0] is y[1]
> True
>
> My understanding was that every literal is a constructure of an object. 
> Thus, the '2.' in 'x = 2.' and the '2.' in 'x is 2.' are different objects. 
> Therefore, the comparison yields false.
>
> But my understanding does not explain the result of the second comparison. 
> According to the experiment, y[0] and y[1] are the same object!

I'm as baffled as you, even more so its implication:

>>> a = 2.
>>> b = 2.

>>> a is b
False

>>> a, b = 2., 2.
>>> a is b
True



More information about the Python-list mailing list