【发布时间】:2017-03-22 18:33:44
【问题描述】:
类似于How to distinguish an instance method, a class method, a static method or a function in Python 3?,我想判断给定的方法是类方法还是静态方法。
在该答案中,描述了如何打印type 以确定这一点。例如,
class Resource(object):
@classmethod
def parse_class(cls, string):
pass
@staticmethod
def parse_static(string):
pass
# I would like to turn these print statements into Booleans
print type(Resource.__dict__['parse_class'])
print type(Resource.__dict__['parse_static'])
打印输出
<type 'classmethod'>
<type 'staticmethod'>
不过,我想更进一步,写一个布尔表达式来判断一个方法是类还是静态方法。
有什么想法可以解决这个问题吗? (我看过 types 模块,但没有一个类型看起来像 classmethod 或 staticmethod)。
【问题讨论】:
-
也许我误解了你,但你只是想将静态方法的类型分配给一个变量以便与你的方法进行比较吗?
type(staticmethod(None))
标签: python