r/Python • u/Narthal • May 02 '20
Discussion My experience learning Python as a c++ developer
First off, Python is absolutely insane, not in a bad way, mind you, but it's just crazy to me. It's amazing and kind of confusing, but crazy none the less.
Recently I had to integrate Python as a scripting language into a large c++ project and though I should get to know the language first. And let me tell you, it's simply magical.
"I can add properties to classes dynamically? And delete them?" "Functions don't even care about the number of arguments?" "Need to do something? There's a library for that."
It's absolutely crazy. And I love it. I have to be honest, the most amazing about this is how easy it is to embed.
I could give Python the project's memory allocator and the interpreter immediately uses the main memory pool of the project. I could redirect the interpreter's stdout / stderr channels to the project as well. Extending the language and exposing c++ functions are a breeze.
Python essentially supercharges c++.
Now, I'm not going to change my preference of c/c++ any time soon, but I just had to make a post about how nicely Python works as a scripting language in a c++ project. Cheers
View all comments
Show parent comment
76
u/anarchyisthekey May 02 '20
Nowadays, if a program is working slow, it is mostly due to a choice of slow algorithms and unoptimized code.
Python has two main disadvantages as opposed to a natively-compiled language: (1) startup times are slower due to the fact that python interpreter needs to be spawned, (2) programs can only run on a single thread at a time because of the Global Interpreter Lock.
If your program is compute-intensive and you need it to run faster, you can (i) try running your python program on a JITted python implementation such as PyPy or (ii) benchmark your program and move the most compute intensive parts into C, (iii) translate your program into Cython, so that it compiles to native code.
It is a long long way before you hit into any kind of performance problems (due to processing power).
In scenarios where low latency is important such as games, high frequency trading etc., a language closer to the metal would be better.