[issue43325] Documentation should warn that 'is' is not a safe comparison operator for most values.
New submission from Anthony Flury <anthony.flury@btinternet.com>: 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#...; 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. ---------- assignee: docs@python components: Documentation messages: 387692 nosy: anthony-flury, docs@python priority: normal severity: normal status: open title: Documentation should warn that 'is' is not a safe comparison operator for most values. type: enhancement _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Change by Terry J. Reedy <tjreedy@udel.edu>: ---------- versions: +Python 3.10 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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. ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- keywords: +patch pull_requests: +23909 stage: -> patch review pull_request: https://github.com/python/cpython/pull/25168 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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/f8775e4f72240faba3947eea8efdd83ee56... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- nosy: +miss-islington nosy_count: 3.0 -> 4.0 pull_requests: +23919 pull_request: https://github.com/python/cpython/pull/25178 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: 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/de0b2b133011953b02536cc78f2499d5d55... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
Anthony Flury <anthony.flury@btinternet.com> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue43325> _______________________________________
participants (4)
-
Anthony Flury -
miss-islington -
Raymond Hettinger -
Terry J. Reedy