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
If fnis instancemethod, you can use fn.im_class.
fn
instancemethod
fn.im_class
>>> class Foo (object): ... def bar (self): ... pass ... >>> Foo.bar.im_class __main__.Foo
, , (.. @specialTest bar, , , - ).
@specialTest
bar
Python 2 im_class . Python 3 __self__.__class__ ( type(method.__self__)).
im_class
__self__.__class__
type(method.__self__)