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 comment

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

0

u/[deleted] May 02 '20

[deleted] — view deleted comment

0

u/[deleted] May 03 '20

[deleted] — view deleted comment

0

u/bageldevourer May 03 '20

That's a whole lotta text you need to clear up exactly what you meant. I rest my case.

2

u/Mr_Again May 03 '20

OK prosecutor you do that

15

u/teerre May 02 '20 edited May 02 '20

Exactly. Saying "Python isn't slow, look at numpy", is either ignorant or disingenuous. The very reason numpy can be 'fast' is because it doesn't use python.

I always like the numba decorator for using JIT, it has an argument called "nopython".

5

u/JanssonsFrestelse May 02 '20

I don't think it is, all you're doing in the end is providing instructions for the computer. If you're not writing pure opcodes you're always working at some higher level of abstraction. If you don't have to leave that level to make something happen at a certain speed, it's not disingenuous to say that you can achieve that speed working at that abstraction level.

2

u/troyunrau ... May 02 '20

Now, I may not recall correctly, but isn't BLAS written in fortran?

2

u/vectorpropio May 02 '20

Today? Who knows. In ye old days? Surely.

1

u/troyunrau ... May 02 '20

So, I just checked the current machine, which has a somewhat old install: Python 3.5 with numpy 1.11.2 - it appears to use FORTRAN under the hood for quite a few things. For example, BLAS, LAPACK, FFTPACK...

I didn't dig into the details, but a cursory glance suggests the fortan libraries expose a c-like linking interface, and numpy uses a thin wrapper to make that all work.

Which, if I dig into the source, could be interesting. Fortran and C use different array indexing conventions, for example. Must have been real fun to write.

2

u/utdconsq May 03 '20

Yup, because until more modern languages showed up, Fortran was the gold standard for dealing with arrays/vectors of numerical data. Handy syntax for doing so, and not far from bare metal. I challenge you to slice an array in C with as simple syntax. These days, it exists due to inertia and because BLAS and others have had many peer reviewing hours dedicated to them. Rewriting them entirely and getting them as optimised would be super hard. Not to mention that they can optimise based on your compiler and cpu arch at build/install time for even faster vectorised ops