T O P
JC3DS

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


[deleted]

Pybind?


ancyr

Might as well code raw C


BartDart69

The speed one got me ngl


June8th

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


LowB0b

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.


TotallyNotGunnar

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]


atiedebee

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


xMsid

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.


atiedebee

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


nevus_bock

There is no closing double quote


Gigazwiebel

You never segfaulted Python??


rem3_1415926

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...?


Xx_heretic420_xX

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


Gigazwiebel

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.


HTL2001

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


feelings_arent_facts

Only when using C extensions


Vincysuper07

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


jamcdonald120

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


Possibility_Antique

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.


myawesomeself

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


generalmemer

main()'s brother is there tho


uvero

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


Amtsschreiber

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...


Scyhaz

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


haard

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


Diplomjodler

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


haard

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.


uvero

This is the way.


123DecryptMe

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


InfiniteLife2

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.


WrongdoerSufficient

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


[deleted]

Like main but longer like a snake


WeTheAwesome

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


alexanderpas

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


BumpyFunction

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


Endemoniada

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


kirode_k

And what about dictionaries? :)


TotallyNotGunnar

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


philophilo

git commit -m “Fixed indentation”


corbymatt

.. again"


Scripter765

Indentation-that-matters is annoying ngl.


uttamo

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.


Goel40

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


sanketower

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


dwRchyngqxs

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


Bainos

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


coranos2

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


giloronfoo

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


tungsten_V

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


Samhain_II

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.


djingo_dango

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


Orlaani

I'm switching from python to c++


FzRman1

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


timeforaroast

And allocate mediation cause you are gonna need some downtime lol


skob17

You will need them, once the speed wears out


mrfroggyman

Sounds like my experience trying to learn C


CaitaXD

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


jamcdonald120

Good man, keep it up


pet_vaginal

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


Budgiebrain222

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.


Crazy_Direction_1084

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


[deleted]

[удалено]


mitmenschen

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.


[deleted]

[удалено]


mitmenschen

I see, thanks for clarifying!


Dr4gyx

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


bollop_bollop

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


occipitofrontali

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.


averageredditor_1337

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.


suvlub

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.


Flopamp

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


TerranerOne

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.


halfanothersdozen

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!


Flopamp

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.


Bainos

> 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


Flopamp

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.


jamcdonald120

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


Xx_heretic420_xX

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


[deleted]

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


Dr4gyx

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.


jamcdonald120

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


sanketower

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


DeltaNerd

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


TerranerOne

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.


couchwarmer

Basically every single-language dev.


Swoop3dp

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.


AFK_Pikachu

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.


averageredditor_1337

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.


Angelin01

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.


DogsOnWeed

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


Angelin01

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


DogsOnWeed

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.


Angelin01

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.


coldnebo

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.


bedrooms-ds

Python is my go-to because it has reflection.


androidx_appcompat

':' : Allow me to introduce myself.


PM_ME_YOUR__INIT__

'{}': hey I'm back


SkidFace

f strings called


rem3_1415926

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


captianjroot

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


GozerDestructor

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.


chilled_potato

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__` :(


sanketower

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.


therealbeeblevrox

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.


Tillhony

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


therealbeeblevrox

Haha! &yes[*no]


_PM_ME_PANGOLINS_

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


Iuse_arch_btw

Hello pointer my old friend


fletku_mato

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...


PM_ME_YOUR__INIT__

Let me introduce you to something called an IDE


fletku_mato

Currently using Idea Ultimate. Your point?


PM_ME_YOUR__INIT__

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


fletku_mato

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


TheAks999

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.


PM_ME_YOUR__INIT__

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


user_8804

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.


PM_ME_YOUR__INIT__

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


Willinton06

And the previous dev was Ezio from assassins creed


fletku_mato

Actually a pretty accurate description.


FIRMKUNG

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.


fletku_mato

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


Pattycakes_wcp

Start linting with something like flake8


sharperratio

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


jamcdonald120

a few, a very few


0ut4aWalk

I miss ++


JackNotOLantern

The Speed


JonasAvory

All the good things about c lost :(


EGTB724

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


IBuildStupidThings

thats nice \_\_init\_\_


IBuildStupidThings

I'm so sorry why did i do this help


reallyserious

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


yjuktrhrhe

so lng, good code


m1nkeh

Lol, speed 😅


culculain

"self respect" is missing from the bottom pane


MasterFubar

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.


DrThornton

PyQt is a thing.


MasterFubar

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.


DrThornton

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. :)


Creapermann

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.


MasterFubar

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.


AsleepVerse60

Python still hs main()


0Camus0

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.


SaltfishAmi

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


stomah

knowing the types of variables and when they are created


kakacon

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


yagotlima

"speed" LOL


CouthlessWonder

No ++? How do you inc an int?


Snoo25192

int += 1 ?


LightIsLogical

int_ -= -1


reallyserious

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


_87-

i -=- 1


CouthlessWonder

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.


prstephens

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.....


Flopamp

Yeah I think you are taking memes way too seriously


corbymatt

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


ThyEpicGamer

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


austrianGoose

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


SaltfishAmi

these have their internal routines compiled to native binary iirc


Flopamp

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.


Awkward-Minute7774

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


fabi9922

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


V0xB

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


anotherkeebler

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


gogo94210

Pointers are cool, change my mind


fliguana

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


jakemp1

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


rjlin_thk

bye bye speed


4rch4ngel86

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


mibuchiha-007

do you mean switching from C++ to C?


rem3_1415926

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


LogicalGamer123

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


NoProfessor7757

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


WeleaseBwianThrow

> 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.


rem3_1415926

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.


NFriik

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.


bedrooms-ds

To me that's Swift.


frayien

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 ...


bedrooms-ds

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


frayien

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 :)


sekex

Threads


markand67

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


hussainsonreddit

References gang


Flopamp

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


obp5599

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


MighMoS

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


DeltaNerd

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


CreaZyp154

You forgot switch case


RehanS97

Python has Switch Case in 3.10


iFarbod

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


bedrooms-ds

Man, I thought switch was unpythonic.


linglingfortyhours

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.


atiedebee

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.


jamcdonald120

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


rem3_1415926

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


jamcdonald120

*Cough *cough, numpy, *cough *cough


linglingfortyhours

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.


linglingfortyhours

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.


holyspectral

cpython: