6

Possible Duplicate:
Python: Behaviour of increment and decrement operators

Hi, I've tried this.

++num

and the num doesn't change at all, always show the value when initialized

if I change ++num to num+=1 then it works.

So, my question is how that ++ operator works?

Community
  • 1
  • 1
user469652
  • 48,855
  • 59
  • 128
  • 165
  • 2
    I don't see a need to downvote this question. It's a duplicate and the appropriate action is closing not downvoting. – Davy8 Oct 14 '10 at 19:48
  • Doesn't work? What makes you even think it exists? What tutorial are you reading? Where did you see it? – S.Lott Oct 14 '10 at 20:30
  • 1
    @S.Lott: You could just start stabbing away at Python and think it's there because several other languages do. If you just write some function and try to use `++x` as an increment, it won't throw any errors, just be silently broken. – Nick T Oct 14 '10 at 20:53
  • 2
    @Nick T: "stabbing away at Python" and "think" don't belong in the same sentence. It makes for a very, very bad question. And it says bad things about anyone who tries to learn a language by "stabbing away". Indeed, it causes me deep grief to think that folks actually do such things. Reading has every advantage. And it's faster than stabbing away. And there's less opportunity for this kind of toweringly bad assumption. – S.Lott Oct 14 '10 at 20:59
  • unfortunately, I would have to double down on the concept of "stabbing away" at Python. To be honest it's unclear in many new languages, especially ones such as Rust, about the best way to learn something new. The Rust book is really cool but it's also exhaustive and something grueling to read thru. I still haven't finished it, and I'm fairly proficient in Rust at this point. The best way I actually learned it, believe it or not is by actually "stabbing away" at it until it hit home to me. So I don't agree with the above comment , at least as a general cautionary tale as it is conveyed as. – rv.kvetch Apr 10 '22 at 21:16

2 Answers2

27

There isn't a ++ operator in python. You're applying unary + twice to the variable.

Nick T
  • 25,754
  • 12
  • 83
  • 121
14

Answer: there is no ++ operator in Python. += 1 is the correct way to increment a number, but note that since integers and floats are immutable in Python,

>>> a = 2
>>> b = a
>>> a += 2
>>> b
2
>>> a
4

This behavior is different from that of a mutable object, where b would also be changed after the operation:

>>> a = [1]
>>> b = a
>>> a += [2]
>>> b
[1, 2]
>>> a
[1, 2]
Seth Johnson
  • 14,762
  • 6
  • 59
  • 85
  • I don't see how the += behavior you showed is different from any other language with a += operator that translates `x += c` to `x = x + c` Even if the type was mutable, += always creates a new instance instead of mutating. – Davy8 Oct 14 '10 at 19:17
  • 1
    +1 for more informative than the guy with the faster trigger-finger – BlueRaja - Danny Pflughoeft Oct 14 '10 at 19:20
  • 1
    @Davy8: `+=` does *not* make a new object if it's mutable -- only when it isn't. – Daenyth Oct 14 '10 at 19:21
  • Interesting... so in Python `x += y` does not translate to `x = x + y`. That seems counterintuitive to me, but I haven't been bitten by it yet. – Davy8 Oct 14 '10 at 19:25
  • And you are correct, I just tested it. If I do `a += [2]` then I get the result described, but if I do `a = a + [2]` then `b` remains `[1]` and `a` is `[1, 2]`. – Davy8 Oct 14 '10 at 19:27
  • I don't understand how the example demonstrates integer immutability. If I do `int a, b; a = 2; b = a; a += 2` in C with its presumably mutable integers, I get the exact same result. – Nick T Oct 14 '10 at 19:31
  • 1
    Actually, `x += y` *can* translate into `x = x + y` if `x` is an object with `__add__` overloaded but not `__iadd__`. – Mike DeSimone Oct 14 '10 at 19:34
  • @Nick: in Python, effectively every object is a reference. It's different from C. – Seth Johnson Oct 14 '10 at 19:53
  • If it transparently works the same, how is it different? Yeah, I could use pointers with the C code, but the "default" mechanism of variables function similarly. – Nick T Oct 14 '10 at 19:58