如何使用Python装饰器作为标记

时间:2019-10-09 20:09:02

标签: python python-2.7 python-decorators

我想以某种方式用装饰器“标记”某些类方法,以便可以使用自省功能并确定哪些方法具有该装饰器:

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))}

有一种简单的方法吗?

0 个答案:

没有答案