196

A moment of silence to all the python devs that don't get the ++ and -- reference

Comments
  • 18
    "Shouldn't that be +=1?"
  • 8
    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.
  • 8
    @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
  • 1
    @Memeamphetamine I know. I'm a python dev. I had this sudden urge of pointing it out right after I read your rant. Peace :)
  • 4
    @varundey I'm a python dev myself haha
  • 2
    I'm a ruby dev - I also feel this pain
  • 3
    @mattrayner Welcome to devRant! I'm also quite new here
  • 1
    @Memeamphetamine thanks :-)
  • 1
    😂😂😂
  • 3
    Swift developers too, apparently.
  • 3
    @divil it's an expression with side effect. It shouldn't be actively encouraged by the language
  • 4
    Agree++
  • 4
    @ajfaura I'm glad you incremented this agreement
  • 0
    @elazar true, but those who know how to use it well (and understand the difference between var++ and ++var) can write some nifty expressions 😉
  • 0
    I'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?
  • 2
    @garrettw Yes, hence the first comment
  • 0
    @Memeamphetamine I meant as opposed to merely an assignment statement
  • 2
    @garrettw I'm not sure what you mean then
  • 0
    @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.
  • 1
    @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.
  • 1
    @jallman112 thanks, that's exactly what I was looking for - and more 🙂
  • 1
    Those crazy brainfuck devs wondering why it doesn't go up by two.
Add Comment