title: Using callbacks to run function using the current values in a class
body: <p>I struggled to think of a good title so I'll just explain it here. I'm using Python in Maya, which has some event callback options, so you can do something like <code>on save: run function</code>. I have a user interface class, which I'd like it to update when certain events are triggered, which I can do, but I'm looking for a cleaner way of doing it.</p>
  
  <p>Here is a basic example similar to what I have:</p>
  
  <pre><code>class test(object):
      def __init__(self, x=0):
          self.x = x
      def run_this(self):
          print self.x
      def display(self):
          print 'load user interface'
  
  #Here's the main stuff that used to be just 'test().display()'
  try:
      callbacks = [callback1, callback2, ...]
  except NameError:
      pass
  else:
      for i in callbacks:
          try:
              OpenMaya.MEventMessage.removeCallback(i)
          except RuntimeError:
              pass
  
  ui = test(5)
  callback1 = OpenMaya.MEventMessage.addEventCallback('SomeEvent', ui.run_this)
  callback2 = OpenMaya.MEventMessage.addEventCallback('SomeOtherEvent', ui.run_this)
  callback3 = ......
  ui.display()
  </code></pre>
  
  <p>The callback persists until Maya is restarted, but you can remove it using <code>removeCallback</code> if you pass it the value that is returned from <code>addEventCallback</code>. The way I have currently is just check if the variable is set before you set it, which is a lot more messy than the previous one line of <code>test().display()</code></p>
  
  <p>Would there be a way that I can neatly do it in the function? Something where it'd delete the old one if I ran the <code>test</code> class again or something similar?</p>