Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Not to be an A hole but I don't think anybody has learnt a programming language so abstracted that they don't understand the ++ and - - reference.
-
@varundey It's just a joke
But Python doesn't actually support ++, it uses +=1 instead so someone who only recently started programming with python might not understand -
@Memeamphetamine I know. I'm a python dev. I had this sudden urge of pointing it out right after I read your rant. Peace :)
-
elazar10308y@divil it's an expression with side effect. It shouldn't be actively encouraged by the language
-
garrettw2698y@elazar true, but those who know how to use it well (and understand the difference between var++ and ++var) can write some nifty expressions 😉
-
garrettw2698yI'm not a Python dev so someone tell me – can (var += 1) be used as an expression which evaluates to the new value of var?
-
garrettw2698y@Memeamphetamine ok let me try to explain.
var = 2
var += 1
---- so all this does is set var to 3. Simple assignment statements.
var = 1
if ((var += 1) == 2) {
echo 'success'
}
---- this sets var to 1, then adds 1 to it, and compares the result with 2. They are equal, so the text is printed. This is what I meant by working as an expression.
I'm not saying this is necessarily a good way to code, but it is a way. -
@garrettw In Python, assignment is a statement not an expression. "If" requires an expression to evaluate, so this:
if ((var += 1) == 2):
throws an invalid syntax error.
What really is handy about the augmented assignments is that the target is only evaluated once and, when possible, it is updated in place. In a = a + b, a gets evaluated twice. Also a new object is created and assigned to the new a. In a+=b, a gets evaluated only once, and for mutable types it's updated in place without creating a new object.
Since integers are immutable, for something like var+=1 then it's mostly a convenient shorthand.
Related Rants
A moment of silence to all the python devs that don't get the ++ and -- reference
undefined
--
++
decrement
increment
python