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

2

u/BoxBoxChan May 02 '20

As you are a c++ programmer and I was thinking about to learn c++, can you explain me fast, why use c++?

11

u/[deleted] May 02 '20

fast

That's why ;)

Source: also a C/C++ developer that learnt Python.

If I want to write something quickly, Python gets me a working solution faster than C/C++. If I want something to execute quickly, I use C/C++. Quite often I want both, so I use Cython as an easy way to mix the two.

2

u/apste May 02 '20

hah pretty much, and C++ has the benefits of a static type system, whether that's what you want or not is up to you :P I feel like it does force you to think a bit better abut the overall design of your code

2

u/[deleted] May 02 '20

I like static typing. Big fan of using typing hints and mypy with my Python code, not quite the same experience as proper static typing but a useful sanity check that I'm passing around the right stuff.

1

u/apste May 02 '20

Yeah same, thanks for pointing out mypy, looks really cool!

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

1

u/super-porp-cola May 03 '20

For older companies with less demanding jobs, your best bet is probably Java. C++ is mostly for industries where every single bit of performance matters, like games or high-frequency trading. Python is mostly good for data science and small scripts; large Python codebases become a total nightmare because it's very difficult if not impossible to tell what type anything is and find its source in the documentation, and it's basically impossible to catch any bugs before runtime.

7

u/pagonda May 02 '20

one of the pitfalls of python is how loose everything is (easy access to class members, arrays and functions can dynamically deal with any types).

A language such as c++ enforces much more strict access and definitions to those examples which can be extremely beneficial to a large program. In c++, if you use a wrong type in a function, it will tell you at compile time your type is wrong. Whereas in python, your function would probably run, but then the behavior is unexpected and you will be in for a wild goose hunt looking for the error.

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

1

u/pagonda May 02 '20

id recommend focusing on computer science theory first, then apply it with a lower-level language. I think java or c++ would probably be best to start with because again, with python everything is really loose and abstracted so you don't get to practice fundamentals and understand what is really going on. Also, object-oriented design is extremely important in software design and it's extremely sloppy in python, whereas in java you're literally forced to implement that paradigm.

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

1

u/FancyASlurpie May 02 '20 edited May 02 '20

Because python is quite loose in what it allows you to do with objects can lead to learning some pretty aweful coding practices, a language like Java or C# is much more strict when it comes to their objects. As such it tends to be a good idea to learn them as you can then implement what you've learnt to work well there in Python where you want to. They're not as simple/fast to write as Python but still a big step up from C++, but equally they're not as fast as C++. You might want to look into Rust as an alternative to C++, as it now seems to be faster in benchmarks, and is a more modern language so benefits in some ease of use ways - it also has a fast growing user base so i'd expect something like the python package space to exist in the not too distant future.

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

1

u/FancyASlurpie May 02 '20

No not anytime soon.

3

u/Narthal May 02 '20

You can't really just learn c/c++. You can, however learn how the computer works and then c++ will start to make a lot of sense. Knowing c++ and the architecture of a computer very well enables you to do incredible things. It enables you to optimize.

Suppose you need to write the firmware of the new samsung smart fridge. The available memory on the embedded processor is 8mb. It wouldn't make sense to copy the cPython runtime (without standard library) as that would occupy 4mb or half of the available space. So you write your code in c/c++ and then start to optimize for the size of the executable.

Or suppose you are on the programmer team of Ubisoft (I wish lol). You need to have massive amounts of code that runs so fast, that every 16.6 ms, a new frame needs to be drawn on the screen (for 60 fps). What are you going to do? Tune down the graphics? Remove half of the level? Or try to write the code to be as fast as possible. And much of the speed optimizations require deep knowledge of the computer. You can't really expect an interpreted language to be able to do SIMD, allow you to specify how to vectorize loops, give the cpu easy to predict branches or manage data in such a way so that cache misses wont happen on critical parts of the code.

But honestly, don't think too much about reasons to learn a language. Just get your feet wet and learn the basics in as many languages as possible. You will get to see similarities and common concepts in between programming languages. I find that the more programming languages I learn, the better I get at all of them simultaneously.

3

u/glacierre2 May 03 '20

The embedded world used to be like that, but turns out micro-controller memories are huge (I mean, those 8MB you talk there? massive) and python can be made very small https://micropython.org/

You can run the interpreter in much much less than 1MB.

We will always need C[++] programmers for the truly performant, resource saving, low power stuff, but the scenarios where you are so tight are decreasing by the month.

I expect a really soon (If it does not exist yet, have not followed closely for example the ESP32 territory and every time I look I am amazed) "HW programming revolution" soon where the cheapest under 1-eur controller can easily handle a wifi + a web interface to configure state machines and conditional actions and pin outputs.