如何记录将函数作为Sphinx参数的函数?

时间:2015-11-02 02:39:47

标签: python python-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:说明符来表明参数是函数?

1 个答案:

答案 0 :(得分: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