In my program, I have different classes that all inherit from the same one. These classes process different kind of operations, they store the operations in an inherited attribute, and then sometime in the program I push the changes and empty the attribute that collected the operations, because of course I don't want to push the same operations more than once.
Here an example:
class A(object):
ops = []
class B(A):
def __init__(self, val):
self.ops.append(val)
class C(A):
def __init__(self, val):
self.ops.append(val)
for j in range(5):
for i in range(10):
b = B(i)
b.ops = []
for i in range(20):
c = C(i)
>>>len(c.ops)
150
>>>len(b.ops)
0
Where is the error? I am expecting the attribute 'ops' to only have 20 items from the very last loop that ran
How could I structure this problem in a pythonic way otheswise?