我如何用Sphinx记录一个函数作为参数?

2024-09-28 22:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个类似这样的类:

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:说明符来指示参数是一个函数吗?在


Tags: thetoselfwhichnew参数paramdef
1条回答
网友
1楼 · 发布于 2024-09-28 22:24:18

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

我想您可以指定^{}作为类型。但我建议你解释一下它是如何工作的:

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

相关问题 更多 >