I want to create a Python class that can:
- Take a variable as an argument when instantiating
- Store the value of the variable in an attribute
- Store the current value of the variable whenever "update()" method is used.
The code below did not work as intended. How can I update the value of the attribute through a method call, keeping in mind that it must work for arbitrary variables?
class MyObject():
def __init__(self,data):
self.data = data
def update(self):
self.data = data
value = 0
dataobject = MyObject(value)
value = 1
dataobject.update() #NameError: name 'data' is not defined