[issue43325] Documentation should warn that 'is' is not a safe comparison operator for most values.

Anthony Flury report at bugs.python.org
Thu Feb 25 18:11:20 EST 2021


New submission from Anthony Flury <anthony.flury at 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#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.

----------
assignee: docs at python
components: Documentation
messages: 387692
nosy: anthony-flury, docs at 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 at bugs.python.org>
<https://bugs.python.org/issue43325>
_______________________________________


More information about the Python-bugs-list mailing list