【发布时间】:2021-02-13 11:23:35
【问题描述】:
我执行了以下 Python 代码:
class C:
def m1(self):
print('method m1')
def m2(self):
print('method m2')
@classmethod
def m3(cls):
print('class method')
@staticmethod
def m4():
print('static method')
print()
for key, val in vars(C).items():
print(key, '***', val, end=' ')
if callable(val):
print(True)
else:
print(False)
得到以下输出:
__module__ *** __main__ False
m1 *** <function C.m1 at 0x7f3661a62dc0> True
m2 *** <function C.m2 at 0x7f3661a735e0> True
m3 *** <classmethod object at 0x7f3661bd4670> False
m4 *** <staticmethod object at 0x7f3661ab4f10> False
__dict__ *** <attribute '__dict__' of 'C' objects> False
__weakref__ *** <attribute '__weakref__' of 'C' objects> False
__doc__ *** None False
我想知道为什么callable 为@classmethod 和@staticmethod 返回False。
我实际上是在尝试找出类中所有方法的名称,以便我可以使用用户定义的装饰器来装饰该类的所有方法
【问题讨论】:
标签: python python-3.x