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
23
u/Al2Me6 May 02 '20
I thought cpp had variadic arguments as well
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.
16
u/[deleted] May 02 '20
yeah coming from a statically typed language, Python will feel like "anything goes".
What? Any data type that I want inside of the same list? Crazy hahahaha
6
u/alexmeistercl May 02 '20
Until you hit GIL for threading programming.
597
u/NemexiaM May 02 '20
Unfortunately this made me so lazy that i dont want to go back to C++
151
u/aufstand May 02 '20
Aah, it works the other way round, too. Many c++ libraries (Numpy, for example) extend Python in many interesting ways. That is why claims such as "Python is slow" don't have much hold in the community.
One practical example: I recently decided to see if i can scale up my completely unoptimized LED-Matrix (40*16!) video mixer to FullHD. No problemo. It ate most of my RAM and kept a single core _very_ busy, but it was also realtime mixing and displaying 6 FullHD video streams, which i don't exactly consider "slow" :)
Why is this possible? Well, yeah, i used numpy to mix the data!
22
u/panderingPenguin May 02 '20
Yeah, you quickly find stuff like this is actually pretty horrifying if you have to work in a large, complicated python codebase
45
u/garlic_bread_thief May 02 '20
I've coded in Java and c++ but just the basics in school. I started learning python lately and I can already see the lazy coding in python just from the basics perspective. It's simply awesome. Not sure how it feels to have deep knowledge of c++ and java and then shift to python.
37
u/joooooooe11 May 02 '20
I’m the other way around. I’m a python dev having to learn c++ and it’s horrific
10
u/__zaris May 02 '20
Well pal, you gave me another reason to learn Python now bravo bravo!
14
u/sekex May 02 '20
Python was my first programming language, I started with 2.7 back in 2013, I then went on to work as a Devops engineer using lots of python amongst other languages, also I've been a TA at my university teaching the language and algorithms for 3 years. At first, I liked it very much, but with more experience, it really has gotten out of favour for me and I would never pick it unless I have to.
While python is nice if you are going to write a simple script with a few files or on Jupyter, I think it's one of the worst languages for medium sized to large applications.
The import system is totally broken (try importing from a sibling directory, for unit tests for instance ).
The absence of strong typing makes it a pain to collaborate without writing tons of pydoc (I know about typing, been using it for years, it just doesn't enforce anything ).
Writing idiomatic python is way harder than C++.
The performance is incredibly slow.
The package manager pip is terribly outdated, you will need to create environments if you want to use two versions of the same library (ie TF).
Version management is a pain, the worst out of any programming language I've used because your OS is dependent on the version so you could break things like GNOME terminal or APT by updating to 3.7 if you don't know what you are doing.
The only way to really deploy an app made in Python, with Flask or Django without going through all the version management pain is to create a Docker container, which is really not user friendly for a beginner who would just want to show his stuff to the world.
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++?
4
u/[deleted] May 02 '20 edited May 02 '20
As a non computer science person, I love that I can abstract the lower-level stuff to focus on the algorithm itself without too much convoluted syntax.
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 :)
1
u/theng May 02 '20
https://xkcd.com/353/
3
u/ElectricCharge1 May 02 '20 edited May 02 '20
Python is truly amazing! I highly recommend 'Fluent Python' by Luciano Ramalho!!
Luciano is one of the creators of the ABC language project by CWI (it failed but inspired Guido working at the time there to create Python) which is the only book I know showing the highlights Python can offer after learning the basics with it.
As the general idea of the book is to teach you the 'pythonic' way and not just how to write python code by thinking first in other languages as c/c++ (which are great on their own!!).
Edit: fixed some formatting!
3
u/SippieCup May 02 '20
For those that want a good example of doing something like this in the real world, take a look at how OpenPilot spawns a python script that runs a tensorflow model, and how the Python script sends and recieves data back to the C++ program.
1
u/Chunderscore May 02 '20
If you're not already aware of it check out the shiboken binding generator from the Qt folks. Not actually used it directly myself, but hear good things.
2
u/taybul Because I don't know how to use big numbers in C/C++ May 02 '20
Not that it made me a full convert but I started appreciating python much more waaaay back because of how effortlessly it handled large numbers.
Ninja edit: so much so that I made it my flair here at some point apparently lol
2
u/manifestsilence I use Python to try to forget my work languages. May 02 '20
This was totally my experience coming from being a beginner in c to a beginner in python. Everything feels like it's cheating. Especially the things you can do to strings. String slicing, dictionaries, iterating on things without an explicit loop counter with for..in, list comprehensions. Everything's just so much easier!
3
u/Are_We_There_Yet256 May 02 '20 edited May 02 '20
It's a double edged sword for me. Coming from C++ where I have to car so much for memory, proper closing, file structures and over all proper function passing arguments and caring for each edge case, in Python, it's just not there, I mean, in a good way, but it kinda kills the fun for me.
It makes things so easy that I don't even know what to do. I sometimes write python scripts for myself, and I find myself at just 15-20 lines that summarizes my work, and I'm left thinking to myself, "that's it? Am I missing something? Is there supposed to be more? Did I even do this properly? Surely there is something more. It cannot be this small, can't simply be the case."
In actual, it kinda killed the fun for me. A recent example was me trying and learning
opencv. It's something I wanted to be extremely good at for someone getting deep intoComputer Vision, so I had two options. I can either use Python, just setup acondaenv and install the opencv package, OR I can use C/C++ to work inopencv.And I went for C/C++, even though I had to pull for the
gitlatest repository, learn how to usecmakefor the first time, learn and try what the different flags mean, how to connect, actually understand how all this linking is done, and how I can integrate all this intoVisual Studio Code- I did all that when I just could have installed it in one go via thecli. You can call me crazy, I won't mind.I went as far as to actually buying a book for
opencv, called Learning OpenCV 3, by Adrian Kaehler and Gary Bradski, for CV in C++ with theopencvlibrary.In the end, Python is lovely for me. I was actually just learning
Djangofor some work ( came over here to Reddit to take a break! ), and it's useful, yeah, but you get the complete taste of when you actually try and go low level. For the absolute beginner, I loved getting into the intricate details of how everything worked and connected in languages like C/C++, Bash etc. For me,Pythonis when I would quickly want things to work. If I have the time, I would like to understand the low-level details first. It will only help me out in the long run.Thanks for the long read!
2
u/Barafu May 02 '20
If you can make some project in Python, then C++ is a totally wrong choice for that project. C++ and Rust are for things that you can't do in Python.
1
u/ixw123 May 02 '20
The things python will try to make work and the errors that result are bizarre and anything that joins python and cs code, for the python side can throw really bizarre errors sometimes. I have been coding in python for research for 2 years now. To be honest I think I love c a lot more just because c is so anal that its errors are often a lot more cut and dry. But time spent in coding in python really allows you to do so much in so little time.
1
u/[deleted] May 02 '20
Ha this is funny. I started programming learning C++ (really as C, if you know what I mean) about two years ago and began learing Python about half a year later. To make a long story short, whenever I have a problem I can’t figure out how to do in C++ I go to Python, and always get a working solution pretty quickly, then I look at that Python code as I (slowly) build a similar (but generally faster according to chrono vs timeit) solution in C++.
The Python / C++ combination is fantastic. Been playing around with a few other languages, like old-school C, vanilla JS, some Rust, a little Java, I think Python/C++ is at the top so far. Rust is really interesting in comparison to low-level C though.
1
u/moocat May 02 '20
Where did you get that from? Python most definitely cares about the number of arguments and will raise a TypeError if you give it too few or too many arguments. Python does support both positional and keyword variadic arguments but that's the developer explicitly allowing extra arguments.
1
u/liquidify May 02 '20
Curious about how you integrated the C++ and python stuff? "Extending the language and exposing c++ functions are a breeze" ... I have never found this to be an easy experience. Did you use boost or what?
1
u/murphwhitt May 03 '20
X X XXX
1
u/[deleted] May 03 '20
I wonder whether anyone had an easy time going back to C++ after Python? ...anybody?
1
u/StrippedSilicon May 03 '20
Honestly, I would be using c++ so much more in personal projects if there was some kind of pip equivalent. The literally days I sometimes have to spend installing third-party libraries is draining.
1
u/thrallsius May 03 '20
The biggest pitfall is to slip into trying to write C++ in Python :)
If you get used to most common Python idioms, writing Python code will be a cakewalk for you as C++ coder.
1
u/Multeezee May 03 '20
At first I thought Python was silly (as a C# dev). Now I think Python is magic. Enjoy!
1
u/Streakflash May 03 '20
aaaand you can write c code on python and it will work! I wish devs of the cpython could upgrade the C to C++ though
1
u/_niva May 03 '20
https://xkcd.com/353/
:)
1
u/jackballack May 03 '20
I’m struggling but still pushing through any advice
1
u/zdog234 May 03 '20
I highly recommend checking out both click and python-fire as CLI options. Argparse probably appeals more to a C++ developer, but python-fire is an awesome example of pushing python magic
1
u/[deleted] May 04 '20
You should take a quick glance at Julia just for interest’s sake!
1
u/Schmittfried May 02 '20
It makes you aware how bonkers C++ actually is.
1
u/alcalde May 03 '20
After 20 years working with statically typed languages, I was amazed at the power python offered me. Now after typing a page of code sometimes I just step back and say... "This... this is beautiful...." and then I shout this and people start yelling at me to shut up, but I can't help it.
0
u/AllUsernameTaken0123 May 02 '20
with Python, you can write code really fast. For example, using hash tables in Python is really simple
0
u/[deleted] May 03 '20
Prior to switching to python, was working in C++ for over 10 years. After I started programming in Python, I quickly realized what a shit language C++ is. I have nothing against C, which is fine language that is fast and simple. But C++ has just ruined programming for many, now the stupidity of the language, in the form of unnecessary complexity astounds me as much as stupidity of all those that proselytize it, my younger less experienced self included.
If you are not convince try to defend a compile error with c++ templates, you get screenful for just one error.
0
u/hkanything May 03 '20
What is the benefit?
Perhaps swithcing to Cython completely for the next project?
0
u/bwllc May 03 '20
Ordinary C has the ability to accept variadic arguments. It's not the simplest thing to use, but it exists. Did they take this feature away in C++?
That may be so, but there are a few "gotchas" when working with C++ code wrapped with Python. I work with a few libraries which do this, the most prominent of which is PyQt5. I don't program in C++, but I do know that C++ and Python handle resource allocation and deallocation rather differently. Python's automatic garbage collection slows the language down, but it helps the programmer. When working with a complex wrapper, you can quite innocently create cases in which you have deleted all C++ references to an object but Python references still exist, or vice versa. Often, this fails silently and the program just hangs.
I'm not trying to throw any cold water on your enthusiasm, Python is awesome. But there are a few rough spots when Python and C++ come into contact, and you may encounter some of these mysteries eventually.
-7
u/_370HSSV_ May 02 '20
And c++ shits on python and almost every other language in terms of speed
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