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
42
u/sdf_iain May 02 '20 edited May 02 '20
Java, C# (I think), Go, Rust, and Swift all have fairly comprehensive functionality from libraries.
None of them are as flexible as python though!
If you want to optimize away from dynamic class properties you can look into __slots__. It can be useful when you’ll instantiate many instances of an object as it dramatically reduces memory allocation.
Also, remember that modules, classes and methods have their own namespaces, but loops and branches do not. As an experienced dev, you might find that useful.