2009-11-06 156 views
1

檢查,看是否m.im_self是類的工作一些時間,但似乎並沒有100%可靠(例如,如果你在一個方法使用多個裝飾。)如何判斷給定的方法是否是Python中的classmethod或instancemethod?

+0

請提供工作/非工作場景的代碼示例。 – 2009-11-06 13:17:28

+1

閱讀源代碼有什麼問題? – 2009-11-06 13:22:10

+2

相關:http://stackoverflow.com/questions/1259963/python-assert-that-variable-is-instance-method – 2009-11-06 13:34:01

回答

2

如果它是一個綁定的方法在類那麼這是一個classmethod。

from inspect import ismethod, isclass 
def isclassmethod(m): 
return ismethod(m) and isclass(m.__self__) 
相關問題