5 ms·
“Pure Python” commonly means implemented using only the Python language. Something written in pure Python ought to be portable across Python implementations. I
by Bogdanp 4y ago
“Pure Python” commonly means implemented using only the Python language. Something written in pure Python ought to be portable across Python implementations. I was merely pointing out that this line
https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66b1c29b34ed60ddd001c/Lib/collections/__init__.py#L688 https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66...
isn’t exactly pure Python, because, under a different runtime (eg PyPy), the code would take a different path (the “pure Python” implementation of _count_elements[1] instead of the C implementation[2][3]). Yes, it's hard to draw exact lines when it comes to Python, especially as the language is so tied to its implementation. However, I think in this case it's relatively clear that the code that specific line is calling is an optimization in CPython, specifically intended to get around some of the VM overhead. Said optimization comes into play in the OP.
[1]: https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66b1c29b34ed60ddd001c/Lib/collections/__init__.py#L523 https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66...
[2]: https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66b1c29b34ed60ddd001c/Lib/collections/__init__.py#L530 https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66...
[3]: https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66b1c29b34ed60ddd001c/Modules/_collectionsmodule.c#L2284 https://github.com/python/cpython/blob/4395ff1e6a18fb26c7a66...