The CPython implementation seems to have the same modules written both in C (e.g., datetime
in .c
) and also in .py for the same module (e.g., datetime
in .py
).
My question is which version is used when I use datetime in my .py file when using the CPython interpreter? And why are there two module versions in the first place?
-
The C modules only work with implementations like CPython. The pure-Python modules will work with any implementation of Python.Robert Harvey– Robert Harvey2022年05月13日 14:07:29 +00:00Commented May 13, 2022 at 14:07
-
1As CPython is the reference implementation, they are defining the semantics of how each module should work in other implementations.Robert Harvey– Robert Harvey2022年05月13日 14:15:04 +00:00Commented May 13, 2022 at 14:15
-
As an aside, the fact that the reference implementation of Python is available in both C and python (under a permissive license) is all kinds of awesome.Robert Harvey– Robert Harvey2022年05月13日 14:15:36 +00:00Commented May 13, 2022 at 14:15
-
It kind of makes sense now.multigoodverse– multigoodverse2022年05月13日 14:16:10 +00:00Commented May 13, 2022 at 14:16
2 Answers 2
When there are two modules in the standard lib with the same name, what often has happened is that the original module was written in Python. That's because it is a lot easier to prototype and get it working quickly than in lower-level languages.
Later, once a reasonable design has been found and bugs fixed etc, performance may become a focus. It's a good time to write the slow parts in Cython or the C API and speed them up through compilation to machine code. Typically the additions are placed in a _module.so
or DLL and imported from within the original module.py
.
This avoids the work of prototyping/writing the entire thing in the C API, which is quite tedious.
C code is real code, python is interface code.
The c code is the performant code, while the python is for python runtime to correctly handle call sites.
Also use the c code Version to get the most performance.