curiosity about the nature of identity (in python)

James Stroud jstroud at ucla.edu
Wed Jun 28 17:54:09 EDT 2006


Hello all,

What /is/ identity in python? For example, we can always count on

py> None is None
True

But I have noticed that this works for strings:

py> "none" is "none"
True

and, for example, integers:

py> 42 is 42
True

And I have noticed that this works for equivalent expressions:

py> 42 is (40 + 2)
True

So, I'm guessing that the integer 42, or the string "none" is cached 
somewhere and python looks to see if it is in the cache when evaluating 
'is'. My guess is supported with this test:

py> id(42)
149679044
py> id(40+2)
149679044
py> id(7*6)
149679044

So, I guess my question is to what extent is this equivalency 
implementation dependent? Is this equivalency a requirement for a 
legitimate python implementation? Won't these checks slow down 
evaluation of 'is' considerably? Does '==' ever fall back and use 'is' 
(or 'id') for evaluation?

Just curious.

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list