Get Python function owner class from decorator

I have a decorator in PY. This is a method and takes a function as a parameter. I want to create a directory structure based on the passed function. I am using the module name for the parent directory, but would like to use the class name for the subdirectory. I cannot figure out how to get the name of the class that the fn object belongs to.

My decorator:

def specialTest(fn):
    filename = fn.__name__
    directory = fn.__module__
    subdirectory = fn.__class__.__name__ #WHERE DO I GET THIS
+5
source share
2 answers

If fnis instancemethod, you can use fn.im_class.

>>> class Foo (object):
... def bar (self):
... pass
...
>>> Foo.bar.im_class
__main__.Foo

, , (.. @specialTest bar, , , - ).

+8

Python 2 im_class . Python 3 __self__.__class__ ( type(method.__self__)).

+6

All Articles