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. -- 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) -- 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/ 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 :-) Victor -- Night gathers, and now my watch begins. It shall not end until my death. _______________________________________________ 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/JMMII5TCB432WM4SIVN3K2ITENKIAN65/ Code of Conduct: http://python.org/psf/codeofconduct/