On integers in python we can use:
>>> a = 1
>>> a += 2
>>> print(a)
3
But if you look in available methods in the integer object you will not find the related method __iadd__
. E.g. you get an exception if you execute:
a.__iadd__(2) # equal to a += 2
So how is the operator +=
working on integers? And is there a way to reach a builtin method to do the operation so that we can use it in a list comprehension like this:
>>> a = 0
>>> summands = [1, 4, 5, 7, 9]
>>> [a.__iadd__(a,i) for i in summands]
>>> print(a)
3
The problem is that we can use just method calls and not the =
operator in this case.