You can't overload operator for a specific attribute, because :
c.x += 4
# is equivalent to
c.x.__iadd__(4)
So in fact you are calling the __iadd__
operator of list. If you want to be able to do that, you have to create a new class, extending list, and overload operator __iadd__
or __add__
.
class SuperList(list):
def __iadd__(self, other):
if type(other) == list or type(other) == SuperList:
return super(SuperList, self).__iadd__(other)
return super(SuperList, self).__iadd__([other])