Python Problems: You Ain't Gonna Need It
YAGNI
There are two reasons why python is “bad”. First, it is not statically typed like C. Second, it is not as fast as C. Interestingly, these statements are mostly made by C developers. And to these, I have a reply:
Seriously.
Typing
It is very important to have a strong typing, like C, so that we can catch implementation errors before runtime. Python is prone to mistakes because it doesn’t have strong typing.
Except it does.
Here is a piece of code for you:
user_input = input()
if user_input.isdigit():
value = int(user_input)
else:
value = 0
Can you tell me what is the type of the variable value
? What about user_input
? int
and str
you say? But how can you know?
The thing is python does have a strong typing system (not static, but strong dynamic). A decade ago I was already using PyCharm that could infer types from my code, and raise warnings when I would be misusing types. So, the benefit of validating your code before runtime has always been there, if you used proper IDE, tooling, and code review policies. Just like these benefits are in C only if you use a proper IDE, compiler, etc.
Yes, you may write such a python code that even IDE won’t be able to guess types. But you may also write this in C:
const int main[] = {
-443987883, 440, 113408, -1922629632,
4149, 899584, 84869120, 15544,
266023168, 1818576901, 1461743468, 1684828783,
-1017312735
};
and even though main is usually a function, it will compile to a “Hello World!” and no type system will save you. It doesn’t mean you should.
Python type system is clear, unambiguous, and has all the benefits of what C has. Yes, it is not as prohibitive as the C system. But You Ain’t Gonna Need It.
Performance
In C every object is typed, so the compiler can make crazy optimizations. In python, everything is an object, so it can’t. And thus python is slow.
Except it isn’t.
C compilers are not a part of the language standard. Rather, these are performance tools that somebody else made for us to use. Python ecosystem provides such tools too. There are numpy
and scipy
packages for very fast math operations (bonus points for having complex algorithms already implemented, so that you can’t f*ck up implementing them from scratch in C), as well as numba
for optimizing compilation. In my experience, numpy
solves 25% of speed issues, and numba
solves another 5%.
And the rest 70% of the slowdowns happen not because of the language speed, but because of poor algorithmic complexity. No compiler can save developers from themselves, implementing a bad complexity algorithm in a videogame, or even an operating system. In these cases, python even has an advantage because it is clearer and more expressive than low-level C, which may help developers see their mistakes.
Python has tools to make your code fast. You can’t squeeze each last CPU instruction from your python code. But, unless you are an Amazon or a high-frequency trading company, You Ain’t Gonna Need It.
And if your python code needs to be faster, I am available for consultations and training.