conventions/requirements for 'is' vs '==', 'not vs '!=', etc

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue May 20 03:17:02 EDT 2008


notnorwegian at yahoo.se a écrit :
> i am confused.
> 
> x=5
> y=5
> 
> x==y -> True
> x is y -> True
> 
> shouldnt x is y return False since they shouldnt(dont?) point to the
> same place in memory, they just store an equal value?

Python's "variable" do not "store values", they are name to object 
bindings. x = 5 is a shortand for globals()['x'] = int(5), which means 
"create an int instance with value 5 and bind it to the name 'x' in the 
global namespace'. wrt/ the identity test yielding true, it's the result 
of a CPython specific optimisation for small integer objects, that are 
cached and reused. Since Python integers are immutable, they can safely 
be shared. Now since it's an implementation specific thing, you should 
by no mean rely on it.




More information about the Python-list mailing list