I have the following:
class Trade:
def __init__(self):
entry = [0, 1, 2, 3]
exit = [1, 2, 3, 4]
met = [0, 0, 0, 0]
self.stats = zip(entry, exit, met)
t = Trade()
for entry, exit, met in t.stats:
met = 1
for entry, exit, met in t.stats:
print(entry, exit, met)
I am expecting all values of met to be 1. But that is not the case. What is the reason for this, and how can I make it so that the modifications to met are reflected in the instance of Trade class t. Does the for statement produce a copy of t.stats rather than iterating through its original copy?
When you loop over the values of a list, the values are assigned to local variables. Changing those local variables will not change the original values stored in the list.
Use a
enumerate()call to add indexes, then use that index to change the original list: