Python ProgrammingPython Programming

How to get all methods of a given class which that are decorated in Python?

class awesome(object):
    def __init__(self, method):
        self._method = method

    def __call__(self, obj, *args, **kwargs):
        return self._method(obj, *args, **kwargs)

    @classmethod
    def methods(cls, subject):
        def g():
            for name in dir(subject):
                method = getattr(subject, name)
                if isinstance(method, awesome):
                    yield name, method
        return {name: method for name, method in g()}


class Robot(object):
    @awesome
    def think(self):
        return 0

    @awesome
    def walk(self):
        return 0

    def irritate(self, other):
        return 0


print(awesome.methods(Robot))
Output
{'think': <__main__.awesome object at 0x00000213C052AAC0>, 'walk': <__main__.awesome object at 0x00000213C0E33FA0>}