简体   繁体   English

带有三重单引号和三重双引号的文档字符串有什么区别?

[英]What's the difference on docstrings with triple SINGLE quotes and triple DOUBLE quotes?

I was just wondering what is the difference between two ways of writing Python Docstrings ( __doc__ ):我只是想知道编写 Python Docstrings ( __doc__ ) 的两种方式有什么区别:

  1. three single quotes:三个单引号:

     ''' Comment goes here '''
  2. three double quotes:三个双引号:

     """ Comment goes here """

Is there any subtle difference in the way doc string could be formatted later while generating docs?稍后在生成文档时格式化文档字符串的方式是否有任何细微差别?

No. They are the same.不,它们是一样的。 The only difference is that the first one can contain a sequence of three unescaped double quotes, while the second can contain a sequence of three unescaped single quotes.唯一的区别是第一个可以包含三个未转义的双引号的序列,而第二个可以包含三个未转义的单引号的序列。 (In other words, because the delimiters are different, there is a slight difference in what characters you can use inside them.) (换句话说,由于分隔符不同,您可以在其中使用的字符略有不同。)

Docstrings are just regular strings, and in Python there is no difference between the different string delimiters, except that, of course, you can't use the string delimiter inside the string. Docstrings 只是普通的字符串,在 Python 中,不同的字符串定界符之间没有区别,当然,除了不能在字符串中使用字符串定界符之外。

The informational document PEP 257 -- Docstring Conventions recommends to use """triple double-quotes""" for consistency, and all their examples show the same:信息文档PEP 257 -- Docstring 约定建议使用"""triple double-quotes"""以保持一致性,并且它们的所有示例都显示相同:

For consistency, always use """triple double quotes""" around docstrings.为了保持一致性,始终在文档字符串周围使用"""triple double quotes""" Use r"""raw triple double quotes""" if you use any backslashes in your docstrings.如果您在文档字符串中使用任何反斜杠,请使用r"""raw triple double quotes""" For Unicode docstrings, use u"""Unicode triple-quoted strings""" .对于 Unicode 文档字符串,请使用u"""Unicode triple-quoted strings"""

Whether to use single quotes or double quotes is only a stylistic issue in practice.使用单引号还是双引号只是实践中的文体问题。 There will be no difference in formatting when generating and publishing docs from these strings.从这些字符串生成和发布文档时,格式没有区别。

Choose whatever style you want.选择你想要的任何风格。 Personally I use single quotes everywhere I can in Python.就我个人而言,我在 Python 中的任何地方都使用单引号。

The documentation states:文件指出:

"String literals can be enclosed in matching single quotes (') or double quotes (")."

It doesn't matter which one you decide to use.您决定使用哪一种并不重要。 What does matter, is that you stick with your decision .重要的是,你坚持你的决定 It is good practice to choose a style and stick with it.选择一种风格并坚持下去是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM