-1

I have this code:

class Doh:
    ctr = 0;
    def __init__(self):
        self.my_num = self.ctr
        self.ctr += 1
d1 = Doh()
d2 = Doh()
print(f"d1: {d1.my_num}")
print(f"d2: {d2.my_num}")

I run it expecting d1 and d2 to have different values for self.my_num but they both come out 0:

d1: 0
d2: 0

So clearly I don't get how class variables work with +=. What am I doing wrong?

Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
  • 2
    You meant to update `Doh.ctr` – Thierry Lathuille Sep 04 '19 at 16:19
  • 1
    `ctr` is a static class variable, and is distinct from `self.ctr`. See here: https://stackoverflow.com/a/68672/1643973 – jjramsey Sep 04 '19 at 16:21
  • The += is working right. just do `print(d1.ctr)` to see that. – Peyman Sep 04 '19 at 16:22
  • @Prune I'm not convinced that this question is asking that. This seems like a mutability question to me (how come after `b=0; a=b; b+=1`, `a != 1`?) – Adam Smith Sep 04 '19 at 16:23
  • 2
    I *think* you have confused class and instance variables. You set up a class variable, but never refer to it. You use an instance variable, and complain that it doesn't affect its counterpart in another instance. – Prune Sep 04 '19 at 16:23
  • Not really a duplicate as my self-answer would have shown. – Ray Salemi Sep 04 '19 at 16:24
  • But I was unable to post the answer because the question was closed, prematurely in my opinion. – Ray Salemi Sep 04 '19 at 16:24
  • 1
    You state that you expect the two instance variables to have different values. I infer that you expect the increment statement to update the class variable. The duplicate I cited shows how to properly use those class variables. – Prune Sep 04 '19 at 16:26
  • Yes. That was close to my answer @Prune. Though my confused was caused by nested classes. I wrote up an explanation but still cannot post it. – Ray Salemi Sep 04 '19 at 16:28
  • I figured out what I was doing wrong. Short answer. It should look like this: ``` class Doh: ctr = 0; def __init__(self): self.my_num = Doh.ctr Doh.ctr += 1 d1 = Doh() d2 = Doh() print(f"d1: {d1.my_num}") print(f"d2: {d2.my_num}") ``` Long answer that confused me. `Doh` in the above example was actually a class defined within another class in my code (call it `WrappingClass`. So when I tried `Doh.ctr` I got a complaint that `Doh` wasn't found. That was because it should have been `WrappingClass.Doh.ctr` in my code – Ray Salemi Sep 04 '19 at 16:28
  • I reopened, but it was immediately re-closed as a (different) dupe – Adam Smith Sep 04 '19 at 16:28
  • @AdamSmithThis raises a meta question for me in that I do search for dupes before posting but never find anything close to what I'm asking. Searching seems to be much more effective from Google than inside Stack Exchange. – Ray Salemi Sep 04 '19 at 16:29

1 Answers1

-1

This is a mutability problem. If you replace your 0 with a [] and start appending to it, you'll get the result you expect. However you can't mutate a number, you can only replace it with another one. While

self.my_num = self.ctr

Does in fact save the same reference to both self.my_num and self.ctr, the next line

self.ctr += 1

Replaces self.ctrs reference with a new (incremented) value.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 1
    This is not about mutability. Please refer to the answer from @wim for details: https://stackoverflow.com/a/41369664/6890912 – blhsing Sep 04 '19 at 16:34