title: Python decorator as class member
body: <p>I want to create a decorator for class functions that will produce logging messages at the start and end of the functions with information specific to the class instance running the function. I've tried doing this by creating the decorator as class member, but that doesn't work because the decorator expects the function as the first argument, but as a class method it needs to be <code>self</code>. Here is what I've written so far that doesn't work:</p>
  <pre><code>import functools, logging
  
  logging.basicConfig(format='%(asctime)s - %(levelname)s - %(funcName)s:%(message)s', 
                              datefmt='%Y-%m-%d %I:%M:%S %p',
                              level=logging.DEBUG)
  
  class Foo:
      def __init__(self, bar):
          self.bar = bar
          
      def log(func):
          @functools.wraps(func)
          def wrapper_log(*args, **kwargs):
              logging.info(f&quot;Started (instance {self.bar})&quot;)
              func(*args, **kwargs)
              logging.info(f&quot;Finished (instance {self.bar}&quot;)
              return func(*args, **kwargs)
          return wrapper_log
          
      @log
      def test(self, a, b, c):
          pass
      
  foo = Foo(&quot;bar&quot;)
  foo.test(1, &quot;b&quot;, [3, 4])
  </code></pre>
  <p>I can't move it out of the class and pass the class instance as an argument by decorating the class functions with <code>@log(self)</code> because <code>self</code> doesn't exist outside of the functions. How can I do this?</p>