This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Unexpected behaviour of 'is' operator
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Digin Antony, steven.daprano, xtreak
Priority: normal Keywords:

Created on 2019-09-01 14:25 by Digin Antony, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.png Digin Antony, 2019-09-01 14:25
Messages (3)
msg350952 - (view) Author: Digin Antony (Digin Antony) Date: 2019-09-01 14:25
The 'is' operator behave differently on two sets of values 
please find the attachment below

tested environment 
windows 7
python 3.7.3


Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> a=256
>>> b=256
>>> a is b
True
>>> a=257
>>> b=257
>>> a is b
False
msg350953 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-09-01 14:31
This will emit a SyntaxWarning in Python 3.8 to use == instead of using is for literals. This is not a bug but an implementation detail over caching a range of integers at https://github.com/python/cpython/blob/1f21eaa15e8a0d2b0f78d0e3f2b9e5b458eb0a70/Objects/longobject.c#L19
msg350966 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-09-02 00:38
Further to Karthikeyan Singaravelan comment, the behaviour you see is absolutely correct. The operator isn't behaving differently, it is reporting precisely the truth.

The ``is`` operator tests for object identity, not equality. Python makes no promises about object identity of literals. If you use an immutable literal in two places:

    a = 1234
    b = 1234

the interpreter is free to use the same object for both a and b, or different objects. The only promise made is that ``a == b``.

The Python interpreter currently caches some small integers for re-use, but that's not a language guarantee, and is subject to change without warning. It has changed in the past, and could change again in the future.

The bottom line is that you shouldn't use ``is`` except to test for object identity, e.g. ``if obj is None``.
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82182
2019-09-02 00:38:23steven.dapranosetnosy: + steven.daprano
messages: + msg350966
2019-09-01 14:36:27serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2019-09-01 14:31:49xtreaksetnosy: + xtreak
messages: + msg350953
2019-09-01 14:25:17Digin Antonycreate