最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中的doc string如何写和标准格式是什么

Python crifan 3938浏览 0评论

想要给自己的crfianLib.py中的新增函数,加上doc string的描述。

但是除了知道基本的”””xxx”””之外,其他格式,比如参数等,不知道标准格式应该是什么样的。

python doc string

python中的文档字符串(docString) – 泥土 – 博客园

Docstring – Wikipedia

Pydoc – Wikipedia

PEP 257 — Docstring Conventions | Python.org

def complex(real=0.0, imag=0.0):
    “””Form a complex number.
    Keyword arguments:
    real — the real part (default 0.0)
    imag — the imaginary part (default 0.0)
    “””
    if imag == 0.0 and real == 0.0:
        return complex_zero
    …

Example Google Style Python Docstrings — napoleon 0.6.0 documentation

“””Example Google style docstrings.
This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.
Example:
    Examples can be given using either the “Example“ or “Examples“
    sections. Sections support any reStructuredText formatting, including
    literal blocks::
        $ python example_google.py
Section breaks are created by resuming unindented text. Section breaks
are also implicitly created anytime a new section starts.
Attributes:
    module_level_variable1 (int): Module level variables may be documented in
        either the “Attributes“ section of the module docstring, or in an
        inline docstring immediately following the variable.
        Either form is acceptable, but the two should not be mixed. Choose
        one convention to document module level variables and be consistent
        with it.
Todo:
    * For module TODOs
    * You have to also use “sphinx.ext.todo“ extension
.. _Google Python Style Guide:
“””

coding style – What is the standard Python docstring format? – Stack Overflow

说是支持很多种格式:

  • Epytext:javadoc

  • reST:最常见的,最普遍的,Sphinx支持

  • Google:Sphinx支持

  • Numpydoc

PEP 287 — reStructuredText Docstring Format | Python.org

Using Docstrings to Specify Types – Help | PyCharm

PyCharm中是推荐写法是:

正好我此处也是用的PyCharm。

-》后来无意间发现,PyCharm可以帮忙自动生成基本的doc string

-》输入”””后,回车,自动出现:

【总结】

然后最后就用这种

  • 据说最流行的

  • Sphinx可以解析的

  • PyCharm也支持的

doc string格式了:

def generateMd5(strToMd5) :
    “””
    generate md5 string from input string
    eg:
        xxxxxxxx -> af0230c7fcc75b34cbb268b9bf64da79
    :param strToMd5: input string
    :return: md5 string
    “””
    encrptedMd5 = “”
    。。。。。
    logging.debug(“encrptedMd5=%s”, encrptedMd5)
    #encrptedMd5=af0230c7fcc75b34cbb268b9bf64da79
    return encrptedMd5

转载请注明:在路上 » 【已解决】Python中的doc string如何写和标准格式是什么

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.200 seconds, using 23.38MB memory