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

Steven D'Aprano steve at pearwood.info
Mon May 6 15:12:24 CEST 2013


On Mon, May 06, 2013 at 05:25:24PM +1000, William ML Leslie wrote:
> On 6 May 2013 17:03, Amaury Forgeot d'Arc <amauryfa at gmail.com> wrote:
> > 2013/5/6 Armin Rigo <arigo at tunes.org>
> >>
> >> On Sun, May 5, 2013 at 10:40 PM, Michael Hudson-Doyle <micahel at gmail.com>
> >> wrote:
> >> > I want to say something about negative zeroes here....
> >>
> >> Right: on floats it's not actually the usual equality, but equality of
> >> the bit pattern (using float2longlong).
> >
> >
> > Except for NaN...
>
> It's perfectly acceptable for NaN to `is` on their bit pattern.

Not unless the implementation caches floats. Otherwise you could have two distinct instances with the same bit pattern.

NANs are no different from other floats in that the language doesn't guarantee that there is only one of them. Unless an implementation ensures that there is *exactly one* float object with a given bit pattern, then you can have multiple instances of a specific NAN, and two NANs with the same bit pattern may be distinct objects.

Although... a thought comes to mind. Since floats are immutable, you could add an abstraction between the actual objects in memory as seen by the low-level implementation, and what are seen as distinct objects by high-level Python code. So two floats with the same bit-pattern in two different memory locations could nevertheless be seen by Python as one instance.

I have no idea whether this is plausible, or if PyPy already does this, or whether I'm talking sheer nonsense.

Of course the IDs would have to be the same, and that's tricky, but I guess that's what this thread is about.



-- 
Steven


More information about the pypy-dev mailing list