On Fri, 2004-12-03 at 09:01, Ishwor wrote:
> hi all,
>  can anyone tell me why this distinction? i mean why it returns False
> on floats??
> >>> a = 1
> >>> b = 1
> >>> a is b
> True
> >>> a = 1.1
> >>> b = 1.1
> >>> a is b
> False
> >>> 
> 
> thanx .
> -- 
> cheers,
> Ishwor Gurung


In Python, some integers and strings are interned.  

1 is 1 is true because 1 and 1 are "simplified" to point to the same
object.  

>>> 2 is (1+1)
True
>>> 102 is (101+1)
False
>>> 102 == (101+1)
True

Why?  To quote the Python/C API Reference Manual (section 7.2.1) 

The current implementation keeps an array of integer objects for all
integers between -1 and 100, when you create an int in that range you
actually just get back a reference to the existing object. 

When strings are compiled, they are interned, so ... 
>>> "abc" is "abc"
True
>>> "abc" is ("ab" + "c")
False
>>> "abc" == ( "ab" + "c" )
True
>>> "abc" is intern("ab" + "c" )
True


Is does not measure equality ... the _is_ operator returns true if its
two parameters internally have the same pointer value and are the same
object.

Floating point values are not interned, so 1.1 is 1.1 is false.


Adam DePrince 


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to