How to find the containing class of a decorated method in Python
<p>I am trying to implement a decorator class which would decorate methods in other classes. However, I need the class which holds the decorated method available in the decorator. I can't seem to find it anywhere.</p>

<p>Here's an example:</p>

<pre><code>class my_decorator(object):

  def __init__(self, arg1, arg2):
    print(self.__class__.__name__ + ".__init__")
    self.arg1 = arg1
    self.arg2 = arg2

  def __call__(self, my_callable):
    print(self.__class__.__name__ + ".__call__")
    print(type(my_callable))
    self.my_callable = my_callable
#    self.my_callable_method_class = ?where to get this?

    def function_wrapper(*args, **kwargs):
      print(self.__class__.__name__ + ".function_wrapper")
      print(self.arg1)
      self.my_callable.__call__(*args, **kwargs)
      print(self.arg2)

    return function_wrapper


class MyClass(object):

  @my_decorator(arg1="one", arg2="two")
  def decorated_method(self):
    print(self.__class__.__name__ + ".decorated_method")
    print(type(self.decorated_method))
    print("hello")


m = MyClass()
m.decorated_method()
</code></pre>

<p>That will print out this:</p>

<pre><code>my_decorator.__init__
my_decorator.__call__
&lt;type 'function'&gt;
my_decorator.function_wrapper
one
MyClass.decorated_method
&lt;type 'instancemethod'&gt;
hello
two
</code></pre>

<p>In the decorator class the callable is of type function, while inside the class itself it is of type instancemethod. I can get the im_class from instancemethod, but there is no such thing in function.</p>

<p>How can I get the class containing the decorated method from within the decorator?</p>

<p>I could do this:</p>

<pre><code>class my_decorator(object):

  def __init__(self, cls, arg1, arg2):

.
.

class MyClass(object):

  @my_decorator(cls=MyClass, arg1="one", arg2="two")
  def decorated_method(self):

.
.
</code></pre>

<p>But I would not like to do that because it's redundant and not nice.</p>

<p>Or should I implement this some other way? I basicly need a couple of arguments to the decorator, and I need the class of the decorated method in the decorator.</p>
