title: How to Write a valid Class Decorator in Python?
body: <p>I just wrote a class decorator like below, tried to add debug support for every method in the target class:</p>
  
  <pre><code>import unittest
  import inspect
  
  def Debug(targetCls):
     for name, func in inspect.getmembers(targetCls, inspect.ismethod):
        def wrapper(*args, **kwargs):
           print ("Start debug support for %s.%s()" % (targetCls.__name__, name));
           result = func(*args, **kwargs)
           return result
        setattr(targetCls, name, wrapper)
     return targetCls
  
  @Debug
  class MyTestClass:
     def TestMethod1(self):
        print 'TestMethod1'
  
     def TestMethod2(self):
        print 'TestMethod2'
  
  class Test(unittest.TestCase):
  
     def testName(self):
        for name, func in inspect.getmembers(MyTestClass, inspect.ismethod):
           print name, func
  
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~'
        testCls = MyTestClass()
  
        testCls.TestMethod1()
        testCls.TestMethod2()
  
  
  if __name__ == "__main__":
     #import sys;sys.argv = ['', 'Test.testName']
     unittest.main()
  </code></pre>
  
  <p>Run above code, the result is:</p>
  
  <pre><code>Finding files... done.
  Importing test modules ... done.
  
  TestMethod1 &lt;unbound method MyTestClass.wrapper&gt;
  TestMethod2 &lt;unbound method MyTestClass.wrapper&gt;
  ~~~~~~~~~~~~~~~~~~~~~~~~~~
  Start debug support for MyTestClass.TestMethod2()
  TestMethod2
  Start debug support for MyTestClass.TestMethod2()
  TestMethod2
  ----------------------------------------------------------------------
  Ran 1 test in 0.004s
  
  OK
  </code></pre>
  
  <p>You can find that 'TestMethod2' printed twice.</p>
  
  <p>Is there problem? Is my understanding right for the decorator in python?</p>
  
  <p>Is there any workaround?
  BTW, i don't want add decorator to every method in the class.</p>