2015-11-02 89 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

相关问题