title: How can I emulate passing a variable by reference to an object in Python?
body: <p>I want to create a Python class that can:</p>
  <ul>
  <li>Take a variable as an argument when instantiating</li>
  <li>Store the value of the variable in an attribute</li>
  <li>Store the current value of the variable whenever &quot;update()&quot; method is used.</li>
  </ul>
  <p>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?</p>
  <pre><code>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
  </code></pre>