文档字符串应该放在装饰器之前还是之后?

发布于 2025-01-09 06:48:41 字数 349 浏览 3 评论 0原文

比较以下内容。

示例 1:装饰器之前的文档字符串。

@app.route("/")
"""
summary
"""
def hello() -> str:
    return "Hello World"

对比示例 2:装饰器后的文档字符串:

"""
summary
"""
@app.route("/")
def hello() -> str:
    return "Hello World"

Compare the following.

Example 1: docstring before decorator.

@app.route("/")
"""
summary
"""
def hello() -> str:
    return "Hello World"

versus example 2: docstring after decorator:

"""
summary
"""
@app.route("/")
def hello() -> str:
    return "Hello World"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2025-01-16 06:48:42

文档字符串应该放在哪里?

文档字符串应该放在函数内部,函数头之后的第一件事:

@app.route("/")
def hello() -> str:
    """
    summary
    """
    return "Hello World"

规范本身(PEP 257)明确表示:

文档字符串是一个字符串文字,作为模块、函数、类或方法定义中的第一个语句出现。

为什么?

这很重要,因为文档字符串不仅仅是一种约定。

如果将它们放在正确的位置,您可以使用 < 查看函数文档code>help() function (甚至可能是其他工具):

>>> @app.route("/")
... def hello() -> str:
...     """
...     summary
...     """
...     return "Hello World"
... 
>>> help(hello)
Help on function hello in module __main__:

hello() -> str
    summary

发生这种情况是因为,如果字符串文字是函数声明中的第一个内容,解释器会将其设置为 __doc__ 函数的属性:

>>> hello.__doc__
'\n    summary\n    '

help() 基本上只是以良好的格式显示 __doc__ 属性的值。

Where should the docstring go?

The docstring should go inside the function, the first thing after the function header:

@app.route("/")
def hello() -> str:
    """
    summary
    """
    return "Hello World"

The specification itself (PEP 257) makes it explicit:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.

Why?

It is important because docstrings are not just a convention.

If you put them in the right place, you can see the function documentation with the help() function (and maybe even other tools):

>>> @app.route("/")
... def hello() -> str:
...     """
...     summary
...     """
...     return "Hello World"
... 
>>> help(hello)
Help on function hello in module __main__:

hello() -> str
    summary

This happens because, if a string literal is the first thing in the function declaration, the interpreter will set it to the __doc__ attribute of the function:

>>> hello.__doc__
'\n    summary\n    '

help() basically just displays, nicely formatted, the value of the __doc__ attribute.

天暗了我发光 2025-01-16 06:48:42

两者都不应该放在函数标题下,如下所示:

@app.route("/")
def hello() -> str:
    """
    summary
    """
    return "Hello World"

PEP 257更详细地讨论文档字符串的约定。它指出这些文档字符串被设置为给定函数或类的 __doc__ 属性(如上面链接的 PEP 中所述)。

文档字符串是一个字符串文字,作为模块、函数、类或方法定义中的第一个语句出现。 这样的文档字符串成为该对象的 __doc__ 特殊属性。

这些文档字符串在调用 help() 时出现,如下所示:

>>> @app.route("/")
... def hello() -> str:
...     """
...     summary
...     """
...     return "Hello World"
...
>>> help(hello)
Help on function hello in module __main__:

hello() -> str
    summary

您描述的其他展示位置未设置__doc__ 属性,因此与 help() 的行为不同。

Neither, they should go under the function header, like so:

@app.route("/")
def hello() -> str:
    """
    summary
    """
    return "Hello World"

PEP 257 discusses the convention for docstrings in more detail. It states that these docstrings are set to the __doc__ attribute of a given function or class (as described in the PEP linked above).

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

These docstrings appear when help() is invoked, like as follows:

>>> @app.route("/")
... def hello() -> str:
...     """
...     summary
...     """
...     return "Hello World"
...
>>> help(hello)
Help on function hello in module __main__:

hello() -> str
    summary

The other placements you've described do not set the __doc__ attribute, and thus do not have the same behavior with help().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文