[pypy-dev] comparing strings using is does not works

Skip Montanaro skip at pobox.com
Wed Jun 5 14:54:10 CEST 2013


On Wed, Jun 5, 2013 at 7:47 AM, Andrews Medina <andrewsmedina at gmail.com> wrote:
> Hi,
>
> In the code below:
>
>>>> x = "a"
>>>> b = "a"
>>>> x is b
> False
>
> It is a bug or a behaviour?

Behavior.  Interning strings so different bindings point to the same
memory location is an optimization.  There is no guarantee that any
given Python implementation (or even the same implementation in
different versions) will do what you expect.  FWIW, Python 2.7 seems
to "get it right" (for certain user-defined definition of "right"):

>>> x = "a"
>>> y = "a"
>>> id(x)
135958064
>>> id(y)
135958064
>>> x is y
True

Skip


More information about the pypy-dev mailing list