I currently have a class, that has 2 attributes, initial_value
, and current_value
.
Both are lists, and the initial value is set programmatically during run time, and is not always the same.
They start off the same, but throughout the code, the current_value
changes.
I then need to reset it to the initial_value
, but I am wondering the best way to do this without my code ever affecting the initial_value
.
Here is what does not work due to the pointer changing to the same object as initial value
:
class MyClass:
def __init__(self,v1,v2,v3,v4,v5,v6):
## both initial value and current value get set as the same value during run time
self.initial_value = [[v1,v2,v3],[v4,v5,v6]]
self.current_value = [[v1,v2,v3],[v4,v5,v6]]
def Reset(self):
self.current_value = self.initial_value ### what is the best way to fix this?
myclass = MyClass(1,2,3,4,5,6)
# value of current_value will change throughout the program
myclass.current_value[0][0] = 5
# periodically, need to reset
myclass.Reset()
The challenge that I am having is that the Reset
function gets called many many times, so I am trying to come up with the fastest way of achieving this result.
The best I could come up with so far is:
self.current_value = [*self.initial_value]
Being self taught, I just dont know what the most idiomatic, and efficient way of achieving this.
Thanks so much in advanced!