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?

5
  • 2
    I don't see a need to downvote this question. It's a duplicate and the appropriate action is closing not downvoting. Commented Oct 14, 2010 at 19:48
  • Doesn't work? What makes you even think it exists? What tutorial are you reading? Where did you see it? Commented Oct 14, 2010 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. Commented Oct 14, 2010 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. Commented Oct 14, 2010 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. Commented Apr 10, 2022 at 21:16

2 Answers 2

28

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

Sign up to request clarification or add additional context in comments.

Comments

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] 

9 Comments

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.
+1 for more informative than the guy with the faster trigger-finger
@Davy8: += does not make a new object if it's mutable -- only when it isn't.
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.
Actually, x += y can translate into x = x + y if x is an object with __add__ overloaded but not __iadd__.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.