xaappend(1)
modifies the class attribute ca , a list , calling its append method, which modifies the list in place.
xb += 1
actually a shorthand for
xb = xb + 1
since integers in Python are immutable, so they don't have the __iadd__ method (in place). The result of this assignment is to set the attribute b in instance x with a value of 2 (the result of evaluating the right side of the task). This new instance attribute obscures the class attribute.
To see the difference between an on-site operation and an appointment, try
xa += [1]
and
xa = xa + [1]
They will have a different behavior.
EDIT . The same can be used for integers by putting them in a box:
class HasABoxedInt(object): boxed_int = [0]
or
class BoxedInt(object): def __init__(self, value): self.value = value def __iadd__(self, i): self.value += i
source share