About the inner workings of class attributes
Please consider this code: class C: x = 5 def f(self): self.x += 1 c = C() c.f() print(C.x, c.x) Alright, so to my understanding, when you refer to an attribute by self and it doesn't exist in the instance, Python will look in the class if there's a class attribute with that name. This is what should be happening here as well, right? So since the instance doesn't have x, I expected that either I get an error (referenced before assignment) or the class attribute is changed. Instead, although += is used, the instance suddenly gets the incremented value as a new *own* attribute while the class variable isn't touched. So C.x still is 5, and c now suddenly has an x (== 6) of its own. Can someone explain what exactly is going on here?