Writing a class decorator that applies a decorator to all methods

后端 未结 3 1741
情歌与酒
情歌与酒 2020-12-13 16:26

I\'m trying to write a class decorator that applies a decorator to all the class\' methods:

import inspect


def decorate_func(func):
    def wrapper(*args,          


        
3条回答
  •  醉梦人生
    2020-12-13 16:55

    inspect.isclass(meth.im_self) should tell you whether meth is a class method:

    def decorate_class(cls):
        for name, meth in inspect.getmembers(cls, inspect.ismethod):
            if inspect.isclass(meth.im_self):
              print '%s is a class method' % name
              # TODO
            ...
        return cls
    

提交回复
热议问题