檢測結合方法在類(未實例)給定一個類C
與函數或方法f
,我使用inspect.ismethod(obj.f)
(其中obj
是C
一個實例),以找出是否f
綁定方法或沒有。有沒有辦法直接在課堂上做同樣的事情(不需要創建對象)?在此在Python 3
class C(object):
@staticmethod
def st(x):
pass
def me(self):
pass
obj = C()
結果(在Python 3)::我想我需要檢查是否的功能/方法是構件
>>> inspect.ismethod(C.st)
False
>>> inspect.ismethod(C.me)
False
>>> inspect.ismethod(obj.st)
False
>>> inspect.ismethod(obj.me)
True
inspect.ismethod不起作用,因爲這一個班級,而不是靜態的,但我無法輕鬆完成。我想這可以使用classify_class_attrs
來完成,如這裏所示 How would you determine where each property and method of a Python class is defined? 但我希望有另一種更直接的方法。
這隻對Python ** two **有用,而具體詢問Python ** 3 **的問題,沒有未綁定的方法。 –