On Fri, Jan 24, 2020 at 12:36 AM Victor Stinner <vstin...@python.org> wrote:
> Le ven. 24 janv. 2020 à 03:37, Guido van Rossum <gu...@python.org> a > écrit : > > I think this started with a valuable optimization for `x in <list>`. I > don't know if that was ever carefully documented, but I remember that it > was discussed a few times (and IIRC Raymond was adamant that this should be > so optimized -- which is reasonable). > > > > I'm tempted to declare this implementation-defined behavior -- > *implicit* calls to __eq__ and __ne__ *may* be skipped if both sides are > the same object depending on the whim of the implementation. > > CPython current behavior rely on the fact that it's possible to get > the memory address of an object. I don't think that it should be part > of Python language specification, but seen as a CPython implementation > detail. > However, the presence of the `is` operator is not an implementation detail -- it must be present in all implementations. And that's all that's needed. > -- > > In PyPy, you may or may not have an "object". It depends how PyPy > decides to store values. For example, if a list only contains small > integers: PyPy uses a list specialized for integers and store > integers, not objects which stores integers. The JIT compiler can also > emit machine codes which uses directly CPU registers to store > integers: again, objects don't exist in memory. > > The interesting part is that PyPy managed somehow to mimick CPython > small integer singletons :-) > > >>>> x=1 > >>>> y=2-1 > >>>> y is x > True > >>>> id(x), id(y) > (17L, 17L) > Yeah, that's because there's code that depends on this CPython implementation quirk, and PyPy wants to support such code. In any case, the outcome of `x is y` where x and y are numbers with the same type and value is already implementation-defined. Comparisons that want to shortcut == should do whatever `is` does. > -- > > Another example: there is new implementation of Python called "Snek" > which is based on 32-bit snek_poly_t type. It is designed for devices > with 2K of memory: > > https://keithp.com/snek/ > https://lwn.net/Articles/810201/ > I've heard of it. It does not claim to be a full Python implementation though (unlike micropython, which claims compatibility with Python 3.5 or so). The implementation uses the following structure for objects: > > typedef union { > uint32_t u; > float f; > } snek_poly_t; > > It's just 32-bit. At the C level, objects are not passed by references > (pointers), but by "value": copy 32-bit value to pass them as function > arguments, to copy the function result, etc. > > So it's not possible to get an object address, since there is no > "object" just its value :-) > That's interesting. Then it should follow whatever that implementation does for `x is y` when x and y are numbers with the same value (presumably True as long as they've got the same type). -- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/BS36QMRM7QQ4SZNQUUZDNH6GSAHPWIRV/ Code of Conduct: http://python.org/psf/codeofconduct/