2015-11-02 91 views
3

比方說,我有一個類,它看起來是這樣的:如何記錄一個以Sphinx爲參數的函數?

class FunctionCaller: 
    def __init__(self): 
     """A class which can be used to call different functions which take the same 
     parameters 

     """ 
     self.f = lambda a,b: (a,b) 

    def setF(self, new_f): 
     """Set the function to call 

     :param new_f: The new function this object should call 
     :type new_f: func(:class:`.SomeClass`, :class:`int`) 
     """ 
     self.f = new_f 

    def callF(self, a, b): 
     """Call the function this object currently contains 

     :param a: Some value 
     :param b: Some other value 
     """ 
     return self.f(a,b) 

class SomeClass: 
    """Some class which does nothing 
    """ 
    pass 

例如(忽略了一個事實,這可能是壞的編碼風格),我們假設該FunctionCaller將是調用預期功能將SomeClass作爲其第一個參數,並將int作爲其第二個參數。我希望文檔能夠顯示這兩個鏈接。我在示例中定義它的方式起作用,但看起來不太好。

是否有一種方法可以使用:type:說明符來指示參數是函數?

回答

0

有沒有一種方法可以使用:type:說明符來指示該參數是一個函數?

你可以指定types.FunctionType作爲類型,我想。但是我建議你只是解釋它是如何工作的:

def setF(self, new_f): 
    """Set the function to call 

    :param new_f: The new function this object should call. 

    The new function takes two positional parameters: the first of 
    type :class:`.SomeClass` and the second of type :class:`int`. 
    """ 
    self.f = new_f 
+0

我想不出使用'其他任何方式:類型:'在這種情況下,但恕我直言它並沒有真正提供任何有用的信息。有沒有人看到類型是'types.FunctionType'?我更喜歡「散文解釋」。 – mzjn

相關問題