DEV Community

Pavel Morava
Pavel Morava

Posted on • Updated on

Why is CPython the official implementation rather than PyPy?

I've written this post originally for Quora. You should take it with a few grains of salt as the thorough explanation would be more complex. However, the following lines may clear some misconceptions the readers may hold when they are talking about two different things, the specification of a language and its implementation.

CPython is the referential implementation of Python. It does not to be super fast, but it has to be correctly implemented.

This means whenever you are on doubt or you think the specification seems obscure, you simple run CPython and see for yourself how Python is supposed to behave.

Nobody prevents you to write your own implementation, just be sure, at least in theory, the same code has the same results in your runtime compared to official CPython interpreter.

For instance, take the infamous GIL. So many do complain about it, but Global Interpreter Lock is not enforced by the specification. If you manage to keep the functionality of Python, you are free to get rid of it. In practice, however, it was proved several times, amongst others by Pypy itself, the task is far from easy.

The most important part to understand is that referential implementation aims in the first place for correctness, not performance. Any language, which sets its priorities otherwise, would suck a big deal and should be avoided.

Python itself is not fast or slow because it is just the specification of the language. Even the slowest Python interpreter if it adheres to the specification is valid, in contrast to Cython which may make your code run faster, but being a superset of Python, Cython code might not be compliant with this specification, and as such not to be counted as Python.

In theory, it is nice to have PyPy as a drop-in replacement for CPython but as soon as you cannot be sure if the results are not correct due to subtle bug or slightly different behavior of PyPy, the temporary win may become a headache.

Modern Python is a complex beast and it takes a lot of goodwill to believe that two or more implementations will behave identically under all possible conditions.

If you do not trust me, see for yourself here

Standard library modules supported by PyPy.
Note that large parts of the Python library are implemented in pure python, so they don't have to be listed there.
Please just check if it imports. If it imports, it should work

In fact, even CPython may slightly differ on different systems. So this is why Python Software Foundation sticks to CPython and does not intend to hop on PyPy.

Discussion (0)