[Tutor] is v == for small ints

Steven D'Aprano steve at pearwood.info
Mon Dec 14 23:10:15 EST 2015


On Tue, Dec 15, 2015 at 01:54:30AM +0000, Alan Gauld wrote:

> This behaviour should never be relied on since the definition
> of a "small int" is potentially implementation dependant and
> could even change with Python version or platform. Always
> use == to test integers.

It isn't just *potentially* implementation dependent, it certainly is 
implementation dependant.

Different versions of Python have used different rules for which small 
ints are cached, and the rules for when they will be re-used or cached. 
For example, this is with Python 2.7:


py> a = 100001
py> b = 100001
py> a is b
False
py> a = 100002; b = 100002
py> a is b
True


You CANNOT rely on this behaviour, it WILL change from version to 
version.



-- 
Steve


More information about the Tutor mailing list