python - How to indentify if a function being decorated is a function or a method? -


this question has answer here:

how can identify if current function being decorated method (belonging class) or function?

class classa:   @mydecorator     def method(self)     pass    @staticmethod  @mydecorator def function()   pass 

mydecorator need know if decorated function is:

  • a method (is_method)
  • a static method (is_static)
  • a class method (is_classmethod)
  • a global function (is_function)

how can this?

thanks!

is_method = lambda f: hasattr(f, 'im_self')  is_static = lambda f: isinstance(f, types.functiontype)  is_classmethod = lambda f: getattr(f, 'im_self', none) not none  is_function = lambda f: not hasattr(f, 'im_self') 

http://docs.python.org/2/reference/datamodel.html#types


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -