【发布时间】:2019-04-28 14:37:30
【问题描述】:
在迭代类的属性时,我可以看到@classmethod 和@staticmethod 属性,但我不确定如何根据它们的类型通用地识别它们
class DeprecatedClassWithInit(object):
def __init__(self):
pass
def foo(self):
return "DeprecatedClassWithInit.foo()"
@classmethod
def bar(cls):
return "DeprecatedClassWithInit.bar(cls)"
@staticmethod
def bab():
return "DeprecatedClassWithInit.bab()"
属性看起来像:
bab = <function bab at 0x7f354f5711b8> (type = <type 'function'>)
bar = <bound method type.bar of <class 'utils.test_decorators.DeprecatedClassWithInit'>> (type = <type 'instancemethod'>)
foo = <unbound method DeprecatedClassWithInit.foo> (type = <type 'instancemethod'>)
所以实例方法有一个str() == "<unbound method DeprecatedClassWithInit.foo>"
而classmethod有str() == "<bound method type.bar of <class ...>>"
而staticmethod有str() == <function bab at 1232455>
这是识别属性的好方法吗?
【问题讨论】:
标签: python static-methods introspection class-method