我想以某种方式用装饰器“标记”某些类方法,以便可以使用自省功能并确定哪些方法具有该装饰器:
def awesome(m):
""" decorate a method somehow """
...
def is_awesome(m):
""" return True if a method is awesome, False otherwise """
...
class Robot(...):
@awesome
def think(self):
...
@awesome
def walk(self):
...
def irritate(self, other):
...
@classmethod
def get_awesome_methods(cls):
return {name: getattr(cls, name) for name in dir(cls) if is_awesome(getattr(cls, name))}
有一种简单的方法吗?