TL; DR如何查找是否使用@classmethod
或者具有相同效果的函数来定义函数?检查一个函数是否使用@classmethod
我的问题
为了实现一类的装饰,我想检查是否有方法将类作为其第一个参数,例如通过
@classmethod
def function(cls, ...):
我找到了实现通过types
模块检查@staticmethod
的解决方案(isinstance(foo, types.UnboundMethodType)
是False
,如果foo
是静态的,请参阅here ),但并没有找到关于如何为@classmethod
语境
这样做的任何事情,我试图做的是沿着
def class_decorator(cls):
for member in cls.__dict__:
if (isclassmethod(getattr(cls, member))):
# do something with the method
setattr(cls, member, modified_method)
return cls
和我做的东西线不知道如何实施我在此示例中所称的isclassmethod
这仅适用于Python 3 –
对于Python 2和Python 3(对于个人而言,我使用2.7)都有效果吗? – hlt