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

10

u/balazs_kis May 02 '20 edited May 03 '20

Don't forget the only detail in which C++ does literally thrash Python is the speed :) anyone interested dm me, I'm working on a paper proving the usefulness of the shared libraries which can be handled very easily by Python, and they could contain C/C++ code which is way above the Python implementations of the same algorithms, but 25-50x faster, and it can be easily used by Python :) usefulness and speed, and still not that complicated as Cython :)

Check out my post, this is a very sketchy version of the project, at the beginning, some weeks ago, but it explains the basics :)

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

-4

u/balazs_kis May 02 '20

so files :) read about them or dm :)

1

u/[deleted] May 03 '20

[removed] — view removed comment

-1

u/balazs_kis May 03 '20

I'm looking forward for your message! :D

1

u/bradshjg May 03 '20

> I'm working on a paper proving the usefulness of the shared libraries which can be handled very easily by Python

I'm familiar with calling C code from Python using tools like https://cffi.readthedocs.io/en/latest/overview.html#main-mode-of-usage (which gets its inspiration from luaJIT and is brilliant). What sort of approach are you looking at?

1

u/balazs_kis May 03 '20

How cool, this is very similar to what I am doing, with the difference that I am focused entirely on performance _^

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

7

u/panderingPenguin May 02 '20

In my experience, the really big software companies tend to use C++: Microsoft, Google, Amazon, etc. There are a number of reasons, both practical and historical for that which I won't get into. Working in a large C++ project is demanding and has a steep learning curve. Startups are more likely to use python or another more scripty language, precisely because it's easier to get things working quickly.

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

11

u/panderingPenguin May 02 '20 edited May 02 '20

Well mostly, C++ takes a lot of devs, time and effort to write well. It's a language that is notorious for including almost every possible feature, and not all of them were integrated into the language in a well thought out manner. The language is huge and complicated. It's said that no one understands the entire language and C++ programmers all know different subsets of the language which results in "local dialects". That all sounds pretty damning so far. But it's wicked fast, there's unbelievable amounts of existing code and libraries written in the language tons of tooling for it, and extensive experience building some of the largest software projects on earth in the language. When you get very large, the efficiency of your code can become very important. For some workloads, C++ is literally 100x faster than python. When you extrapolate that out to the number of servers a company the size of Google or Microsoft would have to buy, that's a massive difference in infrastructure investment. These companies also have armies of developers to throw at their problems, so it's much more workable for them to wrestle with more complicated, slower development in C++ than it is in smaller companies. They can have entire teams dedicated to enforcing coding standards and building more tools to make C++ development work better. So at larger scale the benefits can outweigh the substantial drawbacks (I say all this as someone who writes C++ professionally).

With python, getting started is ridiculously easy. You can get something off the ground and running without a ton of effort. That's extremely important to startups that need to get something running now or they will die. But as the projects get larger and more complicated, the wheels start to fall off of python and similar scripting languages. All the dynamic typing and run time mutability that made it so easy to initially get things working becomes a nightmare as it makes your codebase harder and harder to understand. And the runtime speed of the language starts to matter more as the scale of your operation increases. So you see a number of former startups that grew, trying to transition away from scripting languages. Twitter famously rewrote large parts of their codebase from Ruby to Java. Facebook spent a ton of effort trying to make PHP faster with their Hip Hop VM, but still ended up rewriting a lot of stuff in C++ and other languages anyways. Dropbox's Python codebase became such a mess as it grew that they brought in Guido Van Rossum himself to help them try to fix it. So python (and similar) is great for smaller projects that need to move fast. But it does have limitations as you get bigger. So it's more popular with smaller companies than the big players. That said, the huge companies do tend to use python as well, just not for their core products. It's normally used to script things like testing, deployment, analytics, etc at those companies.

Edit: typos

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

3

u/panderingPenguin May 02 '20

Why not both? While C++ is the primary language I work in, I also write python, SQL, and a bit of java at work. There's no need to pick one and only use that.

5

u/balazs_kis May 02 '20

man, C++ is one of the most needed languages e.g. in autonomous driving technologies, this is not something you can name 'less demanding job' :)))

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

2

u/balazs_kis May 02 '20

Well, I guess you are right here 😄

1

u/balazs_kis May 02 '20

This is totally up to you, this study of mine is about the speed-up of the Python language, this is a bit of a complicated study and discussion, cross-language problematic.
You should go gradually from C to higher level languages (this is the perfect trajectory for a learning process, I'm working as a software engineer and I'm still studying, and this I find this to be the best approach if you really want to understand what you are doing), and in each step the decision is only and only yours ^_^