如何记录python函数参数类型?

sor*_*rin 16 python doxygen

我知道参数可以是任何对象,但对于文档而言,指定您期望的内容非常重要.

首先是如何指定下面的参数类型?

  • str(或使用Stringstring?)
  • int
  • list
  • dict
  • 功能()
  • tuple
  • 类的对象实例 MyClass

第二,如何指定可以是多种类型的参数,比如可以处理单个参数的函数,int或者可以是str

请使用以下示例演示使用您提出的解决方案记录此文档所需的语法.请注意,希望能够从文档内部超链接对"Image"类的引用.

def myMethod(self, name, image):
    """
    Does something ...

    name String: name of the image
    image Image: instance of Image Class or a string indicating the filename.

    Return True if operation succeeded or False.
    """
    return True
Run Code Online (Sandbox Code Playgroud)

请注意,只要能够处理要求,欢迎您使用任何文档工具(sphinx,oxygen,...).

更新:

它表示在doxygen中记录参数类型有一些支持.一般.下面的代码可以工作但是会给param名称添加一个烦人的$(因为它最初是为php制作的).

    @param str $arg description
    @param str|int $arg description
Run Code Online (Sandbox Code Playgroud)

Ton*_*ony 13

有一个更好的办法.我们用

def my_method(x, y):
    """
    my_method description

    @type x: int
    @param x: An integer

    @type y: int|string
    @param y: An integer or string

    @rtype: string
    @return: Returns a sentence with your variables in it
    """

    return "Hello World! %s, %s" % (x,y)
Run Code Online (Sandbox Code Playgroud)

而已.在PyCharm IDE中,这有很大帮助.它就像一个魅力;-)

  • 1)要创建功能文档,请按功能标题中的alt + Enter并选择"插入文档字符串存根"https://www.jetbrains.com/pycharm/webhelp/creating-documentation-comments.html 2)文档字符串的默认格式PyCharm是"重组文本".您可以在File-> Settings-> Python Integrate Tools-> Dostring Format将其更改为Epytext(如上面的Tony Melony列表).请参阅epydoc参考http://epydoc.sourceforge.net/epytext.html (2认同)

小智 6

您需要在Doxygen的Python文档字符串的开头添加感叹号才能正确解析它.

def myMethod(self, name, image):
    """!
    Does something ...

    @param name String: name of the image
    @param image Image: instance of Image Class or a string indicating the filename.

    @return Return True if operation succeeded or False.
    """
    return True
Run Code Online (Sandbox Code Playgroud)


rob*_*ert 5

如果使用 Python 3,您可以使用PEP 3107中描述的函数注释。

def compile(
   source: "something compilable",
   filename: "where the compilable thing comes from",
   mode: "is this a single statement or a suite?"):
Run Code Online (Sandbox Code Playgroud)

另请参见函数定义


Tom*_*Tom 0

Doxygen 对于 C++ 来说非常有用,但如果您主要使用 Python 代码,您应该尝试一下sphinx 如果您选择 sphinx 那么您所需要做的就是关注pep8