Strange behaviour of 'is'

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 21 16:35:25 EDT 2006


"Fijoy George" <tofijoy at yahoo.co.in> wrote:

> 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.

What gave you that idea? The compiler may or may not fold together 
identical immutable objects when it compiles your code. In your first 
example the comparison is False because the two lines are compiled 
separately. If compiled together it might have returned True:

>>> x = 2.; x is 2.
True

However this is purely an implementation detail. The language doesn't 
actually require either behaviour.






More information about the Python-list mailing list