Integers do not implement __iadd__ (adding in place, for += ), since they are immutable. The interpreter reverts to its standard destination and __add__ instead, so the line:
self.num += 1
becomes:
self.num = self.num + 1
On the right side, you get foo.num (i.e. 3 ) via self.num , as expected, but the interesting thing here is that assigning the shadow attribute to the instance attribute num class attribute. So the string is actually equivalent:
self.num = foo.num + 1 # instance attribute equals class attribute plus one
All instances end with self.num == 4 , and the class remains foo.num == 3 . Instead, I suspect you wanted:
foo.num += 1
Alternatively, you can implement it as @classmethod , working more explicitly on the class:
class Foo():
source share