I read that some constructs of Python are more efficient because they are compiled in C.
That's not true.
First off, there is nothing in the specification of the Python language that requires that certain functions have to be implemented in a certain language. A Python implementor can choose to implement any function and language construct anyway he wants. For example, in Jython, those functions are implemented in Java, not in C. In IronPython, they are implemented in C#. In PyPy, they might be implemented in RPython or just in Python. In Pynie, they are probably implemented in Python.
Seondly, there is nothing in the specification of the C language that says that it must be compiled. There are interpreters for C.
Thirdly, just because it is in C doesn't mean it's fast. There are C compilers out there which produce really terrible code, and there are pretty fast Python implementations.
And fourth, even if the function implemented in C is blazingly fast, that doesn't necessarily translate into faster execution speed of the overall program. Optimizations typically don't work across languages. For example, the optimizer can't inline the call to map
into your program, because your program is written in Python but map
is written in C. But inlining is pretty much the foundation of all optimizations, because inlining (and loop unrolling) give nice long straight paths of code without branches or calls, which is what optimizers love.
It's generally interpreted,
Actually, all currently existing Python implementations always compile Python code, they never interpret it.
Why not just compile the whole thing?
That's a good question! Writing everything in the same language has many advantages, some of which I outlined above. (Another one is that it's easier to find collaborators who know one language than two.)
If everything is written in the same language, then performance improvements to that language get multiplied throughout the whole system. If everything is written in C, then making the Python 10 times faster isn't going to speed up your program much, because most of the code isn't Python. Sure, the code you wrote is running 10 times faster, but that code mostly consists of calls to C functions which have the same speed as before.
But if everything is written in Python, then making Python faster will have a ripple effect: the primitive types get faster, the datastructures built on top of those primitive types get faster, the algorithms using those datastructures get faster, the modules using those algorithms get faster and so on.
map(something...)
in Python, nothing gets compiled to C. It calls code that was already written in C. Imagine that there was a piece of code in the Python interpreter like this:if(!strcmp(functionName, "map")) {/* mapping code goes here */} else if(!strcmp(functionName, "filter")) {/* filter code goes here */} else if ... else {error("Unknown function!");}
os.getcwd()
for example?os.getcwd
is not builtin. Does that also correspond to some C code embedded in the CPython interpreter as in the case ofmap(something)
?print(os.getcwd)
it prints<built-in function getcwd>
does it not?os
is not builtin, butos.getcwd
is. I used a bad example to illustrate my question, sorry.