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: Documentation should warn that 'is' is not a safe comparison operator for most values.
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: anthony-flury, docs@python, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2021-02-25 23:11 by anthony-flury, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25168 merged rhettinger, 2021-04-03 22:10
PR 25178 merged miss-islington, 2021-04-04 02:55
Messages (5)
msg387692 - (view) Author: Anthony Flury (anthony-flury) * Date: 2021-02-25 23:11
A frequent bug for beginners is to assume that 'is' is somehow 'better' then '==' when comparing values, and it is certainly a cause for confusion amongst beginners as to why:

   [1,2] is [1,2] evaluates to False but
   'a' is 'a' evaluates to True

    and many similar examples.

As far as I can see the first mention of the 'is' operator is under Section 5 - More on conditionals : https://docs.python.org/3/tutorial/datastructures.html?highlight=comparison#comparing-sequences-and-other-types; and it is mentioned thus : 

  The operators is and is not compare whether two objects are really
  the same object; this only matters for mutable objects like lists.

As we know though this statement is misleading - it suggests that 'is' can be used to compare immutable values (ints, strings, tuples) etc, and while for some values of some immutables (small ints, shortish strings) 'is' can be used as an equivalent of '==' it wont be clear from this 'statement' that 'is' is simply not a safe way to compare values.

There needs to be a warning here about 'is' and how it is not an equivalent to '==' in any general sense.
msg387796 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-27 23:49
I'll add a note to this effect in the tutorial.

Also, in the FAQ, we could have an entry about the three circumstances when "is" can relied upon:  1) variable assignment doesn't change identity, 2) containers that use references don't change identity, and 3) singletons don't change identity.
msg390170 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-04-04 02:54
New changeset f8775e4f72240faba3947eea8efdd83ee56ae1fd by Raymond Hettinger in branch 'master':
bpo-43325: Add FAQ entry for identity tests (GH-25168)
https://github.com/python/cpython/commit/f8775e4f72240faba3947eea8efdd83ee56ae1fd
msg390171 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-04-04 03:05
New changeset de0b2b133011953b02536cc78f2499d5d55224f8 by Miss Islington (bot) in branch '3.9':
bpo-43325: Add FAQ entry for identity tests (GH-25168) (GH-25178)
https://github.com/python/cpython/commit/de0b2b133011953b02536cc78f2499d5d55224f8
msg390306 - (view) Author: Anthony Flury (anthony-flury) * Date: 2021-04-06 10:51
Should the data structures page also link to the FAQ. The problem with the FAQ is that most beginners don't even know that == vs 'is' is actually a question they need to ask, and therefore they aren't likely to look at the FAQ in the first place.
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87491
2021-04-06 10:51:11anthony-flurysetmessages: + msg390306
2021-04-04 03:06:15rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-04-04 03:05:59rhettingersetmessages: + msg390171
2021-04-04 02:55:11miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23919
2021-04-04 02:54:54rhettingersetmessages: + msg390170
2021-04-03 22:10:37rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request23909
2021-02-27 23:49:07rhettingersetassignee: docs@python -> rhettinger

messages: + msg387796
nosy: + rhettinger
2021-02-27 03:54:42terry.reedysetversions: + Python 3.10
2021-02-25 23:11:20anthony-flurycreate