__iadd__ and fellows missing (Python 2.2)

Tim Peters tim.one at comcast.net
Sat Apr 13 13:49:00 EDT 2002


[Philip Swartzleonard]
> ... any name that happens to gain the status of being that integer
> really just gets another copy of it's pointer.

[Michael Hudson]
> Yep.  This happens for the empty tuple, the empty string and one
> character strings too (and maybe some other I forget now).

The implementation can choose, or choose not, to share memory blobs for any
value of an immutable type, and exactly when & which objects get shared can
(and does) vary across releases.  The only objects guaranteed to be
singletons (meaning you'll always get exactly the same object no matter how
its value is computed) are None, type objects (things the type() function
returns), and in 2.3 True and False.  So, in particular, "is" should never
be used as a test for integer equality, or equality with an empty tuple or
string.

About a year ago, aysncore.py did have a line of the form

     if s is "":

and of course it eventually failed in real life.  Most ways of computing
strings *just happen* today to return "the same" empty string when an empty
string is the result, but not all do.

>>> id("")
6602808
>>> id("abc"[1:1])
6602808
>>> id("" + "")
6602808
>>> id("abc" * 0)   # oops
6602856
>>>

That's not a bug.  It wouldn't be a bug if all of them returned different
objects.  Indeed, it wouldn't be a bug even if two distinct instances of the
literal "" returned different objects.






More information about the Python-list mailing list