class A:
def __init__(self):
self.quantity = 1
def __add__(self, other):
self.quantity += other.quantity
del other # Not deleting.
return self
def __radd__(self, other):
if other == 0:
return self
else:
return self.__add__(other)
def __str__(self):
return f'{self.quantity}'
def __repr__(self):
return f'<A object: {self.quantity}>'
if __name__ == '__main__':
a = A()
b = A()
array = [a,b]
print(sum(array))
print(array)
Result:
2
[<A object: 2>, <A object: 1>]
Expected result:
2
[<A object: 2>]
The function is adding normally but the del
is not deleting the other items in the list.
I try to use the other
after use the del other
and it prints an error. But its not deleting the item from the list.