[Zope3-dev] Nasty bugs

Tim Peters tim@zope.com
Wed, 20 Mar 2002 12:10:28 -0500


[Steve Alexander]
> ...
> What does code typically compare by identity? Here's the ones I can
> think of:
>
>   * Things the code has itself created
>
>   * Classes / types
>
>   * None
>
>   * Small integers

Any code comparing small ints by identity is plain broken:  Python
guarantees nothing about this, and it can (and has) varied across releases.

>   * The empty tuple

Ditto.  I've also seen code using "is" on an empty string; indeed, that was
a miserable bug in asynchat.py, fixed about a year ago.

>>> "abc" * 0
''
>>> "abc" * 0 is ""
0
>>> "abc" * 0 == ""
1
>>>

The same thing will eventually bite code trying to do, e.g., "i is 3" or "t
is ()":  the only immutable objects safe to compare via "is" are the
guaranteed singletons, like type() results and None.