三重双引号v.s.双引号 [英] Triple-double quote v.s. Double quote

查看:181
本文介绍了三重双引号v.s.双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写Python文档字符串的首选方法是什么?

What is the preferred way to write Python doc string?

""""

深入Python 中,作者提供了以下示例:

In the book Dive Into Python, the author provides the following example:

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.

    Returns string."""

在另一个章中,作者提供了另一个示例:

In another chapter, the author provides another example:

def stripnulls(data):
    "strip whitespace and nulls"
    return data.replace("\00", "").strip()

两种语法均有效.对我来说唯一的不同是"""允许我们编写多行文档.

Both syntax work. The only difference to me is that """ allows us to write multi-line doc.

除此之外还有其他区别吗?

Is there any difference other than that?

推荐答案

来自 PEP8样式指南:

  • PEP 257 描述得很好docstring约定.请注意,大多数 重要的是,以多行文档字符串结尾的"应位于 行本身,例如:

  • PEP 257 describes good docstring conventions. Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, e.g.:

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.
"""

  • 对于一个衬里文档字符串,可以在文件末尾保留结尾的"" 同一行.

  • For one liner docstrings, it's okay to keep the closing """ on the same line.

    PEP 257 建议使用三重引号,甚至对于单行文档字符串:

    PEP 257 recommends using triple quotes, even for one-line docstrings:

    • 即使字符串适合一行,也使用三重引号.这 便于以后进行扩展.
    • Triple quotes are used even though the string fits on one line. This makes it easy to later expand it.

    请注意,甚至Python标准库本身也没有始终遵循这些建议.例如,

    Note that not even the Python standard library itself follows these recommendations consistently. For example,

    • abcoll.py
    • ftplib.py
    • functools.py
    • inspect.py

    这篇关于三重双引号v.s.双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆