Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

python - Triple-double quote v.s. Double quote

What is the preferred way to write Python doc string?

""" or "

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("0", "").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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the PEP8 Style Guide:

  • 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 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...