[pypy-dev] x is y <=> id(x)==id(y)

Maciej Fijalkowski fijall at gmail.com
Sun May 5 19:35:50 CEST 2013


On Sun, May 5, 2013 at 1:20 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On 05/05/13 19:59, Armin Rigo wrote:
>>
>> Hi all,
>>
>> I'm just wondering again about some "bug" reports that are not bugs,
>> about people misusing "is" to compare two immutable objects.  The
>> current situation in PyPy is that "is" works like "==" for ints,
>> longs, floats or complexes.  It does not for strs or unicodes or
>> tuples.
>
>
> I don't understand why immutability comes into this. The `is` operator is
> supposed to test whether the two operands are the same object, nothing more,
> nothing less. Immutable, or mutable, it makes no difference.
>
> Now, it may be that *some* immutable objects may (implicitly, or explicitly)
> promise that you will never have two objects with the same value. For
> example, float might cache every object created, so that once you have
> created a float 23.45910234718, it will *always* be reused whenever a float
> with that value is needed. That would be allowed.
>
> But if float does not cache the value, and so you have two different float
> objects, with different IDs, then it is absolutely wrong for PyPy to treat
> `is` as == instead of testing object identity.
>
> Have I misunderstood what you are saying?

Immutability is important because you can't cache immutable objects.

It's true what you're saying, but we consistently see bug reports
about people comparing ints or strings with is and complaining that
they work fine on cpython, but not on pypy. Also, you expect to have
the same identity if you store stuff in the list and then read out of
it - which is impossible if you don't actually have any objects in the
list, just store unwrapped ones.

Cheers,
fijal


More information about the pypy-dev mailing list