Python: 測試函數是否被調用

# helper class defined elsewhere
class CallLogger(object):
   def __init__(self, meth):
     self.meth = meth
     self.was_called = False

   def __call__(self, code=None):
     self.meth()
     self.was_called = True

而後assert CallLogger的was_called爲True就好了。可是這樣的Callable不是個函數:html

isinstance(object, types.FunctionType) # Callable will be False

對於這種Callable獲取參數個數須要用:函數

inspect.getargspec(fn.__call__)
相關文章
相關標籤/搜索