r/Python • • 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

1.7k Upvotes

219 comments sorted by

View all comments

Show parent comments

8

u/[deleted] May 02 '20

Would Golang work as a lazy alternative to C++?

5

u/risajajr May 02 '20

Yes, it is quite impressive too.

8

u/[deleted] May 02 '20

It depends on what you're trying to do. It's not as fast as C++ but it's much easier to use. Another criticism I've heard of it is it's type system is lacking, but that's probably not something you would notice unless if use type systems heavily in your code. I've worked on one web server written in Golang, and I was pleased with the language.

4

u/EnemyAsmodeus May 02 '20

I've been told the only potential alternative that isn't popular, but gives you that capability is called: Rust.

And of course Java.

However, whenever I needed C++, I would look at Cython, I think that's the main way to enhance python apps to be faster.

10

u/[deleted] May 02 '20

Again depends on what you're trying to do. If you care about average performance then simply rewriting a portion of your Python code in Cython is a good solution. But often times you care more about worst case performance than average performance. This is the case when you're working on a webserver that has a latency SLA for all requests. It's not good enough that 90% of requests satisfy the SLA. You need to make sure that 99.999% of requests do. Python becomes problematic for this use case because it will work okay for 90% of requests but then garbage collection will kick, eat up your CPU, and you'll violate your SLA for a percentage of your requests. For situations like this Python is a poor language choice and something like Go would be better, since its garbage collector is designed for this use case.

3

u/EnemyAsmodeus May 02 '20

Over Cython as well? Or did you mean either Cython or Go?

What you said is definitely fascinating (I get drained of energy when I look at Cython or any C++ code nowadays, I used to do C++ and assembly but now I just lose all my energy trying to understand certain parts. Honestly, maybe C++ is even easier to read than some pyx code I saw).

2

u/SuspiciousScript May 03 '20

Check out Kotlin. Seems like the most impressive high-level, statically typed, compiled language out there right now IMO.

1

u/toastedstapler May 04 '20

Go seems to be more performant than java but a bit less than C++

1

u/NemexiaM May 02 '20

Sorry haven't tried Golang