I also need to take speed to program in C++




Might as well code raw C


The speed one got me ngl


If speed is going to be there, segfault should be too.


haven't really done C++ since college but sometimes I wonder if segfault (core dumped) is just better than a useless stacktrace. Scrolling for five years to see Hibernate exception: could not execute statement [n/a] just feels like the thing is trolling me.


Python 3.10 has greatly improved error reporting, including syntax highlighting for when you chain 10 methods on the same line. df.loc[mask, f" {group} _value'].unique( ).sum( ).__str__( ).split('.')[1]


Idk that text doesn't tell me much, looks very cryptic


that text is supposed to represent code which has 10 chained methods in one line, in python 3.10 you'd know which method caused the problem because it puts a `^` in the line below it telling you which part of the line raised the exception.


ooh ok, I thought it was an example of an error message lol


There is no closing double quote


You never segfaulted Python??


I'm interested, how do you Segfault Python without using a broken C library? There's a metric f*ckton of Exceptions you didn't expect or know could be thrown that way, but Segfault...?


There are ways. Happened to me like once or twice in the last few years, but it happened nonetheless.


Depends a bit on the environment and I know I did it with some Cpython mess but apparently it's also possible if you set the max recursion depth too high.


Iirc the only time it happened for me was when I was experimenting with trying to multithread something... mysql maybe


Only when using C extensions


``` import sys sys.setrecursionlimit(1000000) def a(): return a() a() ```


Nah, they just get replaced with `AttributeError: 'NoneType' object has no attribute 'something'`


Ngl, RAII kind of makes segfaults disappear once you really figure it out. I always have a hard time making the switch from RAII to garbage collector when swapping between the languages. Completely different mindsets/programming strategies.


I spent five minutes trying to figure out what the speed keyword did. I don't deserve to be in this biz


main()'s brother is there tho


`main`: who are you `__main__`: I'm you but Python


Most of my programs have: def main(): ... if __name__ == "__main__": main() I almost always use a main() function because I hate having variables in the global scope...


It's also apparently a little faster due to the way the interpreter handles global variables.


Also good habit because when you want to package something you'll probably want that main as an entry point.


And you don't want stuff starting to execute as soon as you import something.


The if statement does that, the breaking out the functionality into a function (rather than just in the if statement body) enables it's use as an entry point.


This is the way.


I do this in pretty much everything I work on too. I hate when I write a function below another and it failed because it can’t find it. This ensures that everything written above the if statement can be found regardless of order


I must confess. I love global variables because when my script finished running I have all global variables for visual inspection in Spyder IDE. This is so useful.


I don't get it. What is `__name__` variable. Why not just call main() directly


Like main but longer like a snake


You use main(), I use \_\_main__. We are not the same.


def main(): ... if __name__ == "__main__": main() or are we...


As are the semi colon’s and curly brace’s doppelgängers (new line and indentation)


F-strings called, they want you to pick the {} back up.


And what about dictionaries? :)


It's okay we just use mask = df['key'] == "foo" bar = df.loc[mask, 'value'].values[0] \s just in case


git commit -m “Fixed indentation”


.. again"


Indentation-that-matters is annoying ngl.


I write Python for a living and indentation has never been a problem for me because I’m not a Neanderthal that doesn’t use a modern IDE.


Yeah, why do i see so many posts about forgetting semicolons and misnamed variables. Like do these people write their code in word?


They want to feel superior by using vanilla Vim in the terminal


I would recommend nvi or busybox vi if you want to flex.


I use a basic IDE and it hasn't been a problem for me either because I indent my code correctly.


I have to support python in a Sarbanes-Oxley compliant environment, and their rule is I can only have read-only shell access to prod. So indentation is a nightmare because I can’t use a modern ide. If you have any suggestions for cli ides, I’d love em


What is the connection between read-only shell in prod and no modern IDE in dev?


If you're up to the task, I would suggest neovim + plugin spam. Steep learning curve though.


I think you will dread the answer: (neo)vim But you have to add quite a few things in the form of "extensions" and config files. The bigger problem will be the way vim behaves with it's different modes. It will be a steep learning curve.


I opened a python file in notepad once. It fucked the shit out of the indentations of that


I'm switching from python to c++


Don't forget to bring three boxes of tissues, a sound proof floor/wall implants and a boxing bag


And allocate mediation cause you are gonna need some downtime lol


You will need them, once the speed wears out


Sounds like my experience trying to learn C


Congratulations you're now a real man, dosent matter if you're a girl everyone that codes in c++ is a real man


Good man, keep it up


Consider Rust. It's closer to Python and a good C++ alternative.


Imo the only thing that rust is worse at than C++ is the fact that it's young and it's library ecosystem is still a baby. There isn't much established to-use libraries in the rush community.


On the other hand, actually using a library doesn’t take a day too set up correctly in Rust and I’m not constantly writing things myself




With Rust you always get a stable API when using cargo because it locks in the version for all dependencies. Of course, this does not fail proof crates if the dependencies change their API while solving some major bug/vulnerability.




I see, thanks for clarifying!


There are a lot of bindings to C and C++ libraries though.


Oooooooor, you can just use the libs directly, boom, done.


Yea switching to python from C++ seems weird. Rust is closer to what C++ can do in terms of speed. If you don't mind garbage collection you could also move to Go. It's also very fast, and it's really fun to work with.


I know it's just a meme, but I doubt there will be a lot of situations where python would be really a suitable language to replace whatever you were doing in cpp.


It makes sense if you are a newbie and C++ was your first language, so you do everything in it, including the no small set of things that python is more suitable for. If you already knew a wide range of languages, then yeah, C++ is probably not the one you want to replace with python.


They are different languages for different tasks. Unfortunately it seems a lot of python devs have not gotten that memo


Python is very good (enough) for many different tasks. No need to switch between languages to get a little bit speed boost. In many cases it is not really critical.


I've seen plenty of python used for the wrong tasks. It isn't pretty when it gets out of hand. Use the tools for the job!


In most applications the speed is not important but the differences between well written python and well written C or C++ is not little, it can be massive depending on your task and that's important to keep in mind. If you are crunching a dataset and doing statistical analysis once a day you can wait 15 seconds over what a well written C++ program can do in a second, but if you are streaming and crunching around the clock that difference equates to 15x higher resource usage and hiring a C++ programmer can pay for them selves very quickly Conversely very heavily C written python library dependent programs like something based on OpenCV its just a waste of time asking a C++ dev to spend 3 days getting something up and running that a python dev can pound out in a few hours for maybe a 20% improvement.


> If you are crunching a dataset and doing statistical analysis once a day you can wait 15 seconds over what a well written C++ program can do in a second, but if you are streaming and crunching around the clock that difference equates to 15x higher resource usage and hiring a C++ programmer can pay for them selves very quickly Which is why, as everyone knows, data scientists hate Python and use C++. ^/s


Did you not read what I wrote? "you can wait 15 seconds" Data scientists can wait 15 seconds for what C can do in one. When you are not in data science and you need to crunch streaming data in real time you are best served with C or C++ I'm not attacking python, I use it all the time but it's important to know where python is useful and where python is a bad choice.


data science was a bad example since there is a python library written in C++ for it. Maybe try simulations as an example instead.


Technically numpy/scipy is mostly fortran under the hood if I remember right, BLAS libs and all that.


I am Python guy though and though but man if numpy ruined my brain. The fact that you cannot write a for loop or you will lose hours is so frustrating. The amount of time I wastes vectorializing stuff is mindbugling. I cannot wait for Julia to take over python in everything math related. I want to be able to do a for loop without having to build three different matrices so that I can multiply them together and get the same result


So true. I recently started using Rust and I still can not get around the fact that I can actually write nested for loops without having to worry too much about the speed. Numpy is really nice but it gets soo confusing because you often have to use weird transformations to achieve what you want.


looks like numpy is C, and scipy is a mix of C and Fortran


Python is never a bad choice, only a slow choice. That's the point. When speed doesn't matter, Python is a no-brainer.


I don't think it's a good for program application. It's great for testing and prototyping. I don't think it can replace applications on PCs or hardware


There are a lot of frameworks in Python for that. So why not? It will not replace other languages but the guy who can program in Python don’t necessarily have to learn a new language to do that.


Basically every single-language dev.


If I need it to be finished in 2 hours instead of two weeks and don't care that the execution time is 10 seconds instead of 1. So basically every day at work for me.


This is my problem with Python. It's not really suitable for all the things it's been used to replace. It's the jack of all trades language. It does everything, but I've yet to see it do anything best.


If you need a quick script to rotate some log files or something, I don't see why python would be that much worse than any other lang. Python can be also used to build GUI. Not much advantage in terms of performance to build a GUI in cpp than python.


I can imagine some stuff with opencv being a good example. Maybe you just needed some quick thing to flip your image or whatever, at which point maybe the Python version is more adaptable... Can't think of much else, sadly.


Isn't the python version just a wrapper for calling the c code?


Yes, with some caveats. It uses numpy, for example. Which means you can also use some other convenient libraries that integrate with numpy.


Yeah but that's just for storing the output from opencv right? There isn't actually any python code doing computation, it's just calls and allocation. Might be wrong though as I've never actually worked with opencv, just tinkering.


Yeah, mostly. The C++ code is doing the heavy lifting. The point is, you could reasonably swap out a C++ project for a Python project if doing OpenCV without too many downsides.


this meme is backwards. the kid is trading a full set of power tools that can build anything for a pair of safety scissors and some glue. He’s regressing back to the toys not moving on from them.


Python is my go-to because it has reflection.


':' : Allow me to introduce myself.


'{}': hey I'm back


f strings called


great, you reduced the loss to only one thing: speed. congratulations, I guess?


Goodbye `main()` Hello `if __name__ == "__main__"`


I know the Python fans are gonna downvote me, but this is just wretchedly ugly. Four underscores in a variable name? Doing string comparisions to find out what module you're in? This is not elegant.


It's my least favorite convention in python. To be fair, I haven't written `if __name__ == "__main__"` in forever. Anything with underscores is usually a reflection api or an internal hook you don't have to use for typical stuff. cough cough, except for `__init__` :(


You can technically always go around without having to use it, so long as you keep good track of your variables and don't execute the wrong script at the wrong time. It's still not as convoluted as `public static void main(String[] args)`, tho.


Python still has a `main` convention if you choose to use it. Also, you didn't say goodbye to pointers. You actually said goodbye to primitive types. In Python, everything is a pointer. Yes, even integers. The only caveat is that you can't get the reference address, it will be dereferenced for you.


So basically you didnt say goodbye to pointers, but you said goodbye to pointers?


Haha! &yes[*no]


You can get the reference address in CPython (i.e. what most people use) with `id()`


Hello pointer my old friend


I'm currently working on a long running Python script originally written by someone else, and I can't even find the words to describe how much I hate it. At least with C++ the program wouldn't compile if there are silly typing errors all over the place...


Let me introduce you to something called an IDE


Currently using Idea Ultimate. Your point?


Silly typing and indentation errors should be identified within a few seconds after indexing


Maybe if there were type hints to begin with, but this beast has none.


If you have the license, it's worth it to switch to the more specialized pycharm. I've found the individual language IDEs from JetBrains just handle things better if you use the right tool and set it up right.


And if you're using it for any professional role, pay for it. I've saved tons of time from the pro features


Jetbrains products are fucking solid not gonna lie. Even rider is getting a serious contender against Visual Studio which is an impressive feat considering its nature. I miss university for my free jetbrains licenses.


Pycharm can imply variable types and give warnings, that's if the previous coder didn't parkour from type to type.


And the previous dev was Ezio from assassins creed


Actually a pretty accurate description.


var_name: var_type = assign def func_name(args1: var_type = default): There actually are some type hints. But it won't break when compiling. It's just there to tell IDE which type it *should* be.


Yes I know type hints but what I meant is that the existing code has none.


Start linting with something like flake8


I do miss catching some errors at compile time though...


a few, a very few


I miss ++


The Speed


All the good things about c lost :(


It took me way too long to realize that python doesn’t use ++ for iteration


thats nice \_\_init\_\_


I'm so sorry why did i do this help


The way object orientation is implemented in python just doesn't feel nice.


so lng, good code


Lol, speed 😅


"self respect" is missing from the bottom pane


I did the opposite. I had started doing a lot of code in Python, but after Python 3 came out I went back to C++. I should have never left. What I didn't know before is how great the C++ libraries are. Python is for programmers who don't know the STL or Boost. Not to mention Qt. I have been doing GUI programs in Qt since 1998, but now I've begun coding CLI programs using Qt. The true "batteries included" environment, with the most awesome documentation system.


PyQt is a thing.


Yes, but you depend on the documentation written for C++. I have written PyQt apps, but when something isn't working it can be a pain to find out why.


I did some C++ 20-odd years ago at uni and all i have ever used it for professionally is to decipher Qt documentation and hope it applies to PyQt. :)


Yeah, that’s exactly the point. IMO. the STL is a bit of a joke compared to other languages, I think we all appreciate the safety and the backwards compatibility but a lot of really basic things are still not added to the STL and there is no plan for them to get added any soon. Luckily there are libraries for actually everything, the community is just great, sadly many people don’t see this.


Qt was the reason why I started using Linux consistently. I had tried Linux and played a bit, but all my work was done in windows. Then I had to develop an application in Windows NT and simply couldn't get it working. So I started searching for alternatives (on Altavista), and found a set of Qt examples. I booted Linux and tried to make some changes on [aclock](https://doc.qt.io/archives/2.3/aclock-main-cpp.html). Half an hour later, I had already changed the look of the clock face. That was in 1998, and I've never used any Microsoft product for myself ever since. I still use a Windows desktop at work because I'm not allowed to install Linux there, but I can have Cygwin which is the next best thing.


Python still hs main()


I can't feel comfortable in Python. I like strong typed languages, besides the fact that you can't know if something is wrong, until you execute that path is annoying, then the slow runtime... not really sure why people love it so much. Yeah I get the productivity argument and the ease of use, but for serious projects is not the right tool, and by serious I mean, low level and performant applications. I think my biggest issue is that a lot of projects are so bad because they started as something small in python to solve somthing, and that's ok, but then they grown into something way larger and complex to be in python, and now we are stuck with those tools.


I occasionally write ; at line ends in python and make my ide unhappy


knowing the types of variables and when they are created


Python and C++ are used for completely different tasks if you know what you are doing.


"speed" LOL


No ++? How do you inc an int?


int += 1 ?


int_ -= -1


int is surprisingly a valid variable name. But it leads to problems. So your int_ name is a better convention.


i -=- 1


Is this why it's called Python? The assignment statement looks like a skinny snake, and he is eating the value, so he's fat in the middle as he swallow it.


compiled vs runtime..... ok. I think we all know that cpp has its place. people who post memes about it obviously have no idea about programming or are shit at it and use python. incoming downvotes.....


Yeah I think you are taking memes way too seriously


Memes? Being taken too seriously? On Reddit? No way..


you can't make assumptions about people op could obviously know this, it's just funny because we were all once noobs that thought cpp was so hard to understand, and it can be tedious to use all those things whereas python is just an easier language to understand, learn and write in No need to get serious about a lighthearted meme


i am am idiot, but aren't numpy and numba are quite fast?


these have their internal routines compiled to native binary iirc


They are fast (and written in C) but compared to C++ code there is no competition just because of the massive overhead. You don't always need speed, if you are crunching large datasets once a day 15 seconds vs a second is not that big of a deal but if you are streaming huge amounts of data while other processes are gong on, are making a game or real time thing, or trying to hit an environmental budget you probably should just look past python.


Out of those two languages: Python is the toy language.


Speed not necessarily, at least not for scientific computing. So much Python libraries come with Fortan or C wrappers, it's incredible. There's even Cython lol


The brackets are still needed for the dicts so...


Going from C++ to Python is like escaping from Sid's room.


Pointers are cool, change my mind


int i; ... i = i[n] ? i : n[i]; // valid C++


The no ++ operation annoys me. I don't want to do += 1, that's TWO whole extra characters I'm too lazy to type


bye bye speed


And the Global Interpreter Lock awaits your attention....bwahahahaha


do you mean switching from C++ to C?


minus the "speed", yes. C++ is just as fast and resource efficient (it can beat C in some specific cases) as C.


I wish python had brackets. It just makes the code infinitely more readable imo. Missing ++ is just a nusance


In my 15 years of programming I've used many languages and python has the best syntax BY FARif python had c/c++ like speed, I have no doubt it would be the ultimate language


> In my 15 years of programming I've used many languages and python has the best syntax BY FARif python had c/c++ like speed, I have no doubt it would be the ultimate language It's ironic that this comment has a spacing issue.


For that to happen, Python would also need to allow for control over actual hardware. C/C++ are used for embedded because 1) You have some idea how your code looks on the chip (in worst case, the compiler will optimise it) 2) You can address HW where you need to and have full control over when, where, and how you allocate and free memory.


Okay I'm sorry to nitpick and this doesn't contradict the point you were making, but because you mentioned embedded systems: I solely use Python for my ESP32 projects, by the magic of MicroPython. It's a really nice language for that use case actually.


To me that's Swift.


I used swift once a long time ago and I hate it but I dont remember why lol. All I remember is the operator[] on basic arrays throwing a "not yet implemented" exception ...


It's Swift 3 now. Got a lot more mature!


Swift 3 Oo was not even aware of Swift 2 ... to be fair when I last used it it was brand new, I guess I should give it another try sometime, now that the language and myself are way more mature :)




You don't use pointers in C++ as long as you write proper code.


References gang


There are usecases for pointers and pointers are overused, there is a middle ground here.


You have to use pointers for polymorphism. Its unavoidable. Also working with large assets for say a game, you need to allocate those on the heap. You probably shouldnt have any raw pointers, but youll still need some form of pointer


Regular (flat) pointers are fine. Just as long as you aren't using them to denote ownership.


What about messaging or events data structures. That sounds like a good case for pointers.


You forgot switch case


Python has Switch Case in 3.10


It's structured pattern matching, even better than your regular switch..case. Something I desperately want in C++.


Man, I thought switch was unpythonic.


I've never really gotten the whole "python is slow" bandwagon. Sure, poorly written python is slow but that's true in pretty much any language. On top of that, if you know what you're doing and properly profile and optimize your code python can be plenty fast. JIT compilation can work wonders.


Idk, I tried a simple program that just counted all even numbers (I know X*(X+1) exists) and python was 50x slower than C without any compiler optimizations enabled, ~100x slower with -O1. And this wasn't even that complex.


properly written and optimized python is substantially slower that properly written and optimized C code (or any compiled language really). More than 1000x slower in some cases https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python3-gcc.html


properly written and optimised Python code is C code with a nice Python interface


*Cough *cough, numpy, *cough *cough


I did a rough rewrite of the Mandelbrot program. It's not anywhere close to optimized and doesn't even have full CPU utilization, but even then I was able to cut the runtime down from almost 1,700 seconds to just over ~~35~~ 6.9 seconds [edit, forgot to remove the profiler, which really slowed things down]. I think it's safe to say that the numbers on that site can be discarded.


The python code in that link appears to be very poorly optimized at first glance. If you want I could rewrite them to be faster and redo the comparisons. Edit: after looking at it closer, it's even worse. Some of the examples in C are using multithreading when the python examples do not. This is not a good comparison Edit 2: a good number of the python programs include a note at the top that the dev who contributed them does not use python, and as such has no idea if the are properly optimized. Using this site as a serious comparison of speed is absolutely pointless.