bgu*_*ach 19 python styles coding-style pep
我找不到任何PEP参考这个细节.函数定义后必须有一个空行?
我应该这样做:
def hello_function():
return 'hello'
Run Code Online (Sandbox Code Playgroud)
或者我是这样做的:
def hello_function():
return 'hello'
Run Code Online (Sandbox Code Playgroud)
使用docstrings时,同样的问题适用:
这个:
def hello_function():
"""
Important function
"""
return 'hello'
Run Code Online (Sandbox Code Playgroud)
或这个
def hello_function():
"""
Important function
"""
return 'hello'
Run Code Online (Sandbox Code Playgroud)
编辑
这就是PEP在空白行中所说的内容,正如FoxMaSk所评论的那样,但它没有在这个细节上说什么.
空白行
使用两个空行分隔顶级函数和类定义.
类中的方法定义由单个空行分隔.
可以使用额外的空白行(谨慎地)来分离相关功能组.在一堆相关的单行(例如,一组虚拟实现)之间可以省略空行.
在函数中使用空行,谨慎地指示逻辑部分.
Python接受control-L(即^ L)换页符作为空格; 许多工具将这些字符视为页面分隔符,因此您可以使用它们来分隔文件相关部分的页面.请注意,某些编辑器和基于Web的代码查看器可能无法将control-L识别为换页符,并且会在其位置显示另一个字形.
mol*_*are 23
阅读Docstring约定.
它说,即使函数非常明显,你也必须编写一行文档字符串.它说:
在docstring之前或之后没有空行.
所以我会编写类似的东西
def hello_function():
"""Return 'hello' string."""
return 'hello'
Run Code Online (Sandbox Code Playgroud)
Rya*_*eer 17
正如@moliware所指出的那样,Docstring Conventions在One-line Docstrings下声明:
在docstring之前或之后没有空行.
但是,它也说(在多行Docstrings下):
插入后一个空行的所有该文档字符串(单行或多行)记录一个类 -一般来说,类的方法彼此通过一个空行分离,并且文档字符串需要从第一方法偏移一个空白行.
我对所有这些的解释:空白行应该永远不会出现在任何文档字符串之前,并且只应该在文档字符串时才跟上它.