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

亡梦爱人 提交于 2019-12-18 19:05:27

问题


I was just wondering what is the difference between two ways of writing 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?


回答1:


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.




回答2:


It doesn't really matter much. But, for what it's worth the examples shown in PEP 257 -- Docstring Conventions all use """triple double-quotes""".

For consistency, always use """triple double quotes""" around docstrings. Use r"""raw triple double quotes""" if you use any backslashes in your docstrings. For Unicode docstrings, use u"""Unicode triple-quoted strings""".




回答3:


Choose whatever style you want. Personally I use single quotes everywhere I can in 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.



来源:https://stackoverflow.com/questions/13081178/whats-the-difference-on-docstrings-with-triple-single-quotes-and-triple-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!