Sometimes we don't want things to change. In the code below, my original Things
are mutated, even though I made a copy of the list containing them. I'm not that surprised, but I'd like to know how to store my original object properties so I can restore my things
list to exactly how it was when first created.
class Thing:
def __init__(self, x):
self.x = x
def __str__(self):
return str(self.x)
things = [Thing(10), Thing(20)]
original_things = things.copy()
for thing in things:
print(thing)
print("All change")
things[0].x = 30
things[1].x = 40
print("Back to the beginning?")
things = original_things
for thing in things:
print(thing)