Message387692
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. |
|
Date |
User |
Action |
Args |
2021-02-25 23:11:20 | anthony-flury | set | recipients:
+ anthony-flury, docs@python |
2021-02-25 23:11:20 | anthony-flury | set | messageid: <1614294680.42.0.115546489096.issue43325@roundup.psfhosted.org> |
2021-02-25 23:11:20 | anthony-flury | link | issue43325 messages |
2021-02-25 23:11:20 | anthony-flury | create | |
|