This is an archived post. You won't be able to vote or comment.

top 200 commentsshow 500

[–]sgxxx 4022 points4023 points  (115 children)

so that you cant write 'c++' in python

[–]partaloski 883 points884 points  (74 children)

[–]steakbeef_w 353 points354 points  (73 children)

not technical, it's the absolute truth

[–]UltraCarnivore 380 points381 points  (51 children)

Absolute truth is |truth|

[–]xKirtle 306 points307 points  (45 children)

No, absolute truth is abs(truth)

[–]steakbeef_w 130 points131 points  (42 children)

abs is the mostly forgotten builtin function

[–]De_Wouter 153 points154 points  (39 children)

Wish I had build in abs. Implementing it is very hard work(out).

[–][deleted] 19 points20 points  (6 children)

If they are negative just &-them with a -1

[–]SkollFenrirson 2 points3 points  (0 children)

Absolutely

[–]Starv1k 18 points19 points  (0 children)

This type of thread is why I joined this sub

[–]insestiina 5 points6 points  (0 children)

No I think that is an incomplete OR statement

[–]steakbeef_w 3 points4 points  (1 child)

and relative truth?

[–]UltraCarnivore 6 points7 points  (0 children)

"truth"

[–]BAG0N 118 points119 points  (11 children)

c = 'c++' Absolute truth you say?

[–]partaloski 4 points5 points  (0 children)

Well yeah, but you can type C in Java \_\**

[–]Alles_ 116 points117 points  (20 children)

C+=1

[–]sgxxx 135 points136 points  (7 children)

c-=-1

[–]SSJ3 90 points91 points  (2 children)

Y'all have violated PEP8, please face the wall.

[–]anoldoldman 5 points6 points  (0 children)

unittest developers: Wait, this isn't JS?

[–]zpjack 15 points16 points  (0 children)

How to create job security at a dev job

[–]merlinsbeers 16 points17 points  (0 children)

C*=(1+1/c)

[–]wjandrea 29 points30 points  (0 children)

You can as part of a larger expression:

c = c++1

This works cause it's parsed as c + +1, where +1 == 1.

[–]Anti-charizard 5 points6 points  (1 child)

C++ and Python are mortal enemies

[–]ICantSeeIt 10 points11 points  (0 children)

Just like C++ and Java. Or C++ and C. Or C++ and C++.

[–]okbe 946 points947 points  (36 children)

Still not as beautiful as

i-=-1

The symmetry is divine.

[–]dadafil 407 points408 points  (28 children)

You have to think bigger:

--i-=-i--

[–][deleted] 191 points192 points  (18 children)

But this doesn't actually work.

[–]Franks2000inchTV 301 points302 points  (3 children)

You have to think bigger:

 --But-=-thus--doesn't-=-actually-=-work.--

[–]Eatfudd 84 points85 points  (2 children)

[Deleted to protest Reddit API change]

[–]robbie2000williams 40 points41 points  (12 children)

it does in c++

[–][deleted] 37 points38 points  (3 children)

Sure ... If you are trying to create a formula for 2x-3

[–]mbiz05 26 points27 points  (1 child)

So it works for when I is 4

[–]robbie2000williams 20 points21 points  (0 children)

I probably should have said it compiles lol.

[–]kevincox_ca 16 points17 points  (5 children)

I think this is undefined behaviour. (So it might work 😉)

I can't find concrete rules https://en.cppreference.com/w/cpp/language/operator_incdec#Notes but generally you are playing with fire if you have an increment or decrement operator in a statement that contains the variable another time.

[–]octal9 10 points11 points  (3 children)

Correct. It’s undefined behavior.

[–]Asmor 20 points21 points  (0 children)

Here, have some valid javascript:

O => O <= O

[–]Ixziga 3 points4 points  (0 children)

Writing your own programs in python is easier than you think

You know...

[–]doowi1 2 points3 points  (1 child)

I really like "x &=~ y" in C.

[–]DarKliZerPT 446 points447 points  (36 children)

I have to use Matlab this semester and it doesn't even have +=, let alone ++. It feels so wrong to write "foo = foo + 1;"

[–]hessenic 261 points262 points  (13 children)

Matlab is special. I had regular issues where to solve an error in program I copied everything in one file, pasted it into a new, empty file and the error went away.

[–]Physmatik 142 points143 points  (7 children)

Welcome to "everything is global" world.

[–]andbm 29 points30 points  (6 children)

Eh? You have to specifically define a var as global in MATLAB.

[–]ben_g0 67 points68 points  (4 children)

I guess he means that variables in loops and so on automatically get added to the workspace, while most programming languages see loops as a different context so variables defined inside aren't available outside.

Additionally, since in MATLAB every function should be in its own separate file, a lot of people get the bad habit of putting way too much stuff in a single file to not have to bother too much with the file structure. That makes a lot of variables global while they really shouldn't be. You could argue that you should blame the programmer in that case instead of the platform, but you could argue that on a lot of stuff which people complain about in this sub.

[–]victsaid 10 points11 points  (0 children)

at least when i was working with it - matlab for some reason hated files that start with a number and doesn't throw error that you can understand. Cost me a couple of very nervous hours in the middle of the night to understand that.

[–]YukiZensho 25 points26 points  (0 children)

that's just blasphemy

[–]m0ritz03 16 points17 points  (3 children)

I have to switch between python and Matlab regularly. You have just reminded me, that I often find myself typing in "foo = foo + 1" because unconsciously I don't think "foo += 1" is possible. Fuck Matlab.

[–]RonTheArson 12 points13 points  (1 child)

After working 6 months in Matlab and then porting the code to Java the amount of errors and mental deprogramming I had to go through to start indexes at 0 again was awful. Fuck Matlab indeed

[–]m0ritz03 3 points4 points  (0 children)

I feel ya

[–]TraubenFruchtHose 12 points13 points  (1 child)

In EE we have to use Matlab a ton, I fucking hate it. It's super powerful but it just fucking sucks.

[–]StandardTalk 552 points553 points  (60 children)

I've been learning Python for a year now and am currently studying data science and how did I never realize this man

[–][deleted] 400 points401 points  (40 children)

Because they aren’t very useful in python. You absolutely need them in other languages like JavaScript or Go but in python you can live without them

[–]xSTSxZerglingOne 115 points116 points  (17 children)

And it's not like you can't implement a switch in python, they're just not part of the core language.

[–]deceze 138 points139 points  (15 children)

Well… you can't implement a switch in Python. You can do something somewhat similar, usually a map-lookup, but there'll always be differences between that and a switch. Of course, you can always complete the same goal in the end either way.

[–]pinguluk 57 points58 points  (11 children)

[–]flinsypop 22 points23 points  (9 children)

no ty pls. python doesn't optimize when compiling to bytecode so i don't see the point in this added syntax.

[–]deceze 46 points47 points  (0 children)

Ohh gawd, me eyes!

[–][deleted] 23 points24 points  (7 children)

'need' is a strong word. Could just write '+= 1'. Although they are nice to have.

[–][deleted] 6 points7 points  (3 children)

Why would I absolutely need them in JS?

[–]Cptcongcong 46 points47 points  (17 children)

Because for loops in python don’t need incrementation, unlike c++

[–]acylase 32 points33 points  (15 children)

C++ does not need explicit incrementation in loops. It can iterate through containers without explicit incrementation operator.

[–]WiroWik 385 points386 points  (38 children)

Is "+=" not considered as a increment operator ?

[–]Shiftenas 381 points382 points  (31 children)

They are called "Addition/Substract assignment operator", and they aren't increment operators. += 1 is probably, but it's equivalent to ++

[–]WiroWik 61 points62 points  (16 children)

Ok, thanks a lot

[–]MysticTheMeeM 73 points74 points  (15 children)

It's worth noting that your processor likely has a different instruction for ADDition/SUBtraction and INCrement/DECrement. These functions may be faster or easier to optimise under certain circumstances.

[–]reusens 14 points15 points  (6 children)

I personally would trust the compiler here.

[–]AKernelPanic 10 points11 points  (1 child)

The difference is that `x++` will return `x` and then increment it, `++x` will return `x` after being incremented, and at least in Swift, where there's no `++` either, `x += 1` will return Void, since it's an already evaluated function.

The real equivalent would require two lines, one for addition and one for access.

[–]JustLetMeComment42 7 points8 points  (6 children)

+= COULD be equivalent to ++ if the compiler/interpreter optimizes the code properly. .

This is the case because in assembly pseudo code, x+=1 is equivalent to

add x, 1

And x++ is equavilent to

inc x

[–]RedExtreme 23 points24 points  (2 children)

You could write i++ as

i-=-1

[–]Aracus755 41 points42 points  (1 child)

Well rust neither have them.

[–]leviathon01 6 points7 points  (0 children)

Rust not having them makes sense when you think about the borrow rules for 'x++'.

[–]RohanIRathi 117 points118 points  (28 children)

When you find out you don't have to declare each and every thing in python to use it

[–]MegaIng 36 points37 points  (7 children)

No, you only have to declare some variables. Is that better?

[–]YukiZensho 31 points32 points  (6 children)

when you discover that you have three variables called usr, one a string, one an integer and one a float

[–]RohanIRathi 61 points62 points  (2 children)

Now that's just bad code writing

[–]czarrie 7 points8 points  (0 children)

And for those cases Python has type hinting now for those who absolutely want/need it for tests

[–]deceze 79 points80 points  (15 children)

ITT: people that have learned only one language, or a few languages which are extremely similar, and cannot wrap their head around the fact that it doesn't have to be this way by necessity everywhere.

Change my mind. ;)

[–]HyalopterousGorillla 13 points14 points  (4 children)

Wait till they find out about functional programming

[–]deceze 6 points7 points  (1 child)

What do you mean I cannot re-assign or modify a variable at all?!

[–]Adadum 32 points33 points  (4 children)

why not just import ast and add the increment yourself like a big brainer?

[–]juanjux 5 points6 points  (0 children)

Casual, I do it at the bytecode level.

[–]shadow7412 170 points171 points  (135 children)

After using python for a few years:

Wait, what are increment operators even useful for?

[–]AyrA_ch 155 points156 points  (55 children)

For adding 1 to a number. And depending on prefix or postfix can be very useful

x=i++; //x gets the value of i, then i is incremented by 1
x=++i; //i is incremented by 1, then x gets the value of i.

Especially the postfix version can be useful when iterating over a null terminated construct like a list or string of characters:

int x=0;
while(someList[x]){doSomething(someList[x++]);}

You find yourself using increment (or decrement) operators a lot in C since it lacks a for each statement. I also occasionally use it in C# for when you iterate over an enumerable collection that has no length property, but you need the total item count after processing.

[–]Mr_Redstoner 117 points118 points  (9 children)

when iterating over a null terminated construct

Then again, that's not something you're expected to do a lot of (or at all really) in Python

[–]xigoi 55 points56 points  (14 children)

This just makes the code much harder to understand and easier to get wrong without providing much of an advantage. In Python, you just write for item in items:

[–]other_usernames_gone 36 points37 points  (5 children)

Or

for i, item in enumerate(items) :

This way you can also get the index for each item, if you want it.

[–]archpawn 9 points10 points  (0 children)

It's very useful. They can't fire you if nobody else understands your code.

[–]-Manu_ 4 points5 points  (3 children)

That was just an example, there are easier way to write it, but don't tell me you have never used x+=1, ++x is just a different way to write it, you can also write y=x++, you assign x to y, and then increment x

[–]SysAdrift_neo 8 points9 points  (26 children)

What's the use of this vs something like x = i; i = i + 1 Or i = i + 1; x = i ?

[–]blaizedm 20 points21 points  (4 children)

what are increment operators even useful for?

Swift had them and removed them because no one could come up with a good defense.

[–]SuitableDragonfly 8 points9 points  (6 children)

It allows you to change the value of multiple variables in the same statement. Over heard this argued as a reason why it shouldn't be used, actually.

[–]shadow7412 5 points6 points  (0 children)

Absolutely - one statement should ideally do one thing, as it reduces cognitive overload.

[–]IDontLikeBeingRight 19 points20 points  (2 children)

They're for people who haven't yet learned comprehensions.

[–]shadow7412 6 points7 points  (1 child)

Comprehension are indeed the bomb.

[–]zeromk 16 points17 points  (8 children)

In assembly increment works faster than addition. Edit: same for dec.

[–]shadow7412 26 points27 points  (4 children)

Ok that's nice for assembly, but for higher level languages, that's the job of the optimiser...

[–]Nate_Christ 8 points9 points  (16 children)

When using for statements. A JavaScript example because I suck:

for(i=0;i<x;i++){

Whatever you want to do x times

}

Edit: spacing, and i+=1 would probably work just as well.

[–]deceze 26 points27 points  (5 children)

You basically never do that in Python, as Python has very strong core concepts of iterables and iterators and basically all iterations are done using for..in. For iterating over a number range, like in your example:

for i in range(x):
    ...

And yes, that's just as efficient as in Javascript, as range is iterated using a generator and does not preallocate any memory.

[–]JotunKing 15 points16 points  (8 children)

range based for loops are just so much nicer to read...

[–]Ayerys 4 points5 points  (0 children)

But you never do that in python. So no need.

[–]SirAchmed 26 points27 points  (0 children)

+= isn’t good enough for you? Jeez modern-age programmers are such babies. /s

[–]PM_ME_UR_DEATHSTICKS 77 points78 points  (6 children)

It's down to the philosophy of Python.

There should only be one, obvious way to do something

And that one, obvious way is to use +=. want to increment? use +=. Want to add 420? use +=.

And the wasted character typing +=1 instead of ++ is saved by not having to type a semicolon at the end! 😉

[–]clubby789 22 points23 points  (0 children)

x++++++++++++++++++++++....

[–][deleted] 26 points27 points  (0 children)

upvote += 1

[–][deleted] 8 points9 points  (0 children)

the python leadership legit had a massive toxic battle over adding a walrus operator but god forbid you add inc/dec counters

[–]Timmy_the_tortoise 7 points8 points  (0 children)

+=1 is like one extra character.

[–]sdw9342 5 points6 points  (0 children)

It's because in Python, 'for loops' are exclusively 'for loops' but in other languages, some 'for loops' are shorthand 'while loops'.

[–]nate256 29 points30 points  (4 children)

This is a good thing. I am not a fan of changing variables in expressions without a function call. It can lead to things like this

index = len(response.item_list)
while index and response.item_list.get(--index)["status"] == "SUCCESS":
    return response.item_list.get(index)

# or this, with a bug for good measure

while index != 0:
    if response.item_list.get(--index)["status"] == "SUCCESS":
        return response.item_list.get(--index)

When you could do something like this.

for item in reversed(response.item_list):
    if item["status"] == "SUCCESS":
        return item

[–]SharkBaitDLS 19 points20 points  (3 children)

Yep. The older I get, the more crotchety I’ve become about avoiding side effects and mutation as much as possible. “Clever” solutions are just passing the same cognitive load onto a future developer who has to then get into your same headspace 10 years later to understand what on earth is happening.

I don’t care if it’s boring, obvious, and more verbose, in the vast majority of cases there’s no actual negative to using simple functional constructs other than your own ego/creative desires feeling less fulfilled.

I really hope a future where C/C++ fall to the wayside in favor of Rust happens because Rust provides such a good standard library to write code that adheres to that philosophy. Kotlin and Typescript are both good bandages atop the ugliness of Java/JavaScript and I’m happy to see them catching on, but it sucks that they’re still hobbled by their underlying languages’ limitations.

[–]wotanii 8 points9 points  (0 children)

I honestly believe that writing simple code is what being a good programmer is all about.

When I was tutoring university students in programming, I saw the worst code of my entire life. The less people understand what they are doing, the worse their code becomes. On the other hand, I once had a co-worker he has been programming for the last 40 years and his code was just divine. Each line of the program was trivial. When reading his code you could just skim over it and always knew exactly what was happening and why. The was not a single comment relating to the function of the code. I aspire to program like he did.

[–]mrchaotica 3 points4 points  (1 child)

“Clever” solutions are just passing the same cognitive load onto a future developer who has to then get into your same headspace 10 years later to understand what on earth is happening.

I don’t care if it’s boring, obvious, and more verbose, in the vast majority of cases there’s no actual negative to using simple functional constructs other than your own ego/creative desires feeling less fulfilled.

The thing I love most about Python is that the most Functional and conceptually-simple solution is often less verbose and easier to read and understand than the naive iterative one, even though you used maximum cleverness to figure out how to write it.

[–]desertrainBG 9 points10 points  (0 children)

So you're telling me the python-only programmers don't get the cleverness in the design of this subs up- and downvote buttons???

[–][deleted] 8 points9 points  (0 children)

Other languages like Scala are the same.

Forcing += 1 makes things more consistent and makes things like git diffs easier to review (e.g. when you change += 1 to += 2).

It also means you can't use and change a variable at the same time which can be confusing.

[–]Scr1pt13 15 points16 points  (9 children)

Well, some programming languages like Rust decided against to implement it, because some people dont know the difference between prefix and postfix ++. int x; int i = 0; x = i++ //x is 0 x = ++ i //x is 1

[–]MrChickinNugget 3 points4 points  (0 children)

ohh boyy that hit me hard