How can I document a function which takes a function as a parameter with Sphinx?

我们两清 提交于 2021-02-08 17:24:42

问题


Let's say I have a class which looks something like this:

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

For example (ignoring the fact that this might be bad coding style), let's assume that the function that the FunctionCaller is going to be calling expects to take SomeClass as its first parameter, and an int as its second. I'd like the documentation to display links to both of those things. The way I've defined it in the example works, but it doesn't look very good.

Is there a way that I can use the :type: specifier to indicate that the parameter is a function?


回答1:


Is there a way that I can use the :type: specifier to indicate that the parameter is a function?

You could specify types.FunctionType as the type, I suppose. But I'd suggest that you just explain how it works:

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


来源:https://stackoverflow.com/questions/33469924/how-can-i-document-a-function-which-takes-a-function-as-a-parameter-with-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!