简体   繁体   English

Python PEP:函数定义后的空行?

[英]Python PEP: blank line after function definition?

I can't find any PEP reference to this detail.我找不到有关此详细信息的任何 PEP 参考。 There has to be a blank line after function definition?函数定义后必须有一个空行?

Should I do this:我应该这样做吗:

def hello_function():
    return 'hello'

or shoud I do this:或者我应该这样做:

def hello_function():

    return 'hello'

The same question applies when docstrings are used:当使用文档字符串时,同样的问题适用:

this:这个:

def hello_function():
    """
    Important function
    """
    return 'hello'

or this或这个

def hello_function():
    """
    Important function
    """

    return 'hello'

EDIT编辑

This is what the PEP says on the blank lines, as commented by FoxMaSk, but it does not say anything on this detail.正如 FoxMaSk 所评论的,这就是 PEP 在空白行上所说的内容,但它没有对这个细节做任何说明。

Blank Lines空行

Separate top-level function and class definitions with two blank lines.用两个空行分隔顶级函数和类定义。

Method definitions inside a class are separated by a single blank line.类中的方法定义由单个空行分隔。

Extra blank lines may be used (sparingly) to separate groups of related functions.可以(谨慎地)使用额外的空行来分隔相关功能组。 Blank lines may be omitted between a bunch of related one-liners (eg a set of dummy implementations).一堆相关的单行代码之间可以省略空行(例如一组虚拟实现)。

Use blank lines in functions, sparingly, to indicate logical sections.在函数中谨慎使用空行来指示逻辑部分。

Python accepts the control-L (ie ^L) form feed character as whitespace; Python 接受 control-L(即 ^L)换页符作为空格; Many tools treat these characters as page separators, so you may use them to separate pages of related sections of your file.许多工具将这些字符视为页面分隔符,因此您可以使用它们来分隔文件相关部分的页面。 Note, some editors and web-based code viewers may not recognize control-L as a form feed and will show another glyph in its place.请注意,某些编辑器和基于 Web 的代码查看器可能无法将 control-L 识别为换页符,而是会在其位置显示另一个字形。

Read Docstring Conventions .阅读文档字符串约定

It says that even if the function is really obvious you have to write a one-line docstring.它说即使该功能非常明显,您也必须编写一行文档字符串。 And it says that:它说:

There's no blank line either before or after the docstring.在文档字符串之前或之后都没有空行。

So I would code something like所以我会编写类似的代码

def hello_function():
    """Return 'hello' string."""
    return 'hello'

As pointed out by @moliware, the Docstring Conventions state, under One-line Docstrings :正如@moliware 所指出的, Docstring 约定One-line Docstrings声明

There's no blank line either before or after the docstring.在文档字符串之前或之后都没有空行。

HOWEVER, it also says (under Multi-line Docstrings ):但是,它还说(在Multi-line Docstrings 下):

Insert a blank line after all docstrings (one-line or multi-line) that document a class -- generally speaking, the class's methods are separated from each other by a single blank line , and the docstring needs to be offset from the first method by a blank line.记录一个类的所有文档字符串(单行或多行)之后插入一个空行——一般来说,类的方法之间用一个空行分隔,文档字符串需要从第一个方法偏移由一个空行。

My interpretation of all this: blank lines should never precede any docstring, and should only follow a docstring when it is for a class.我对所有这些的解释:空行不应该在任何文档字符串之前,并且只应该在类的文档字符串之后。

Projects use different docstring conventions.项目使用不同的文档字符串约定。

For example, the pandas docstring guide explicitly requires you to put triple quotes into a line of their own.例如, pandas 文档字符串指南明确要求您将三重引号放入他们自己的一行中。

Docstrings must be defined with three double-quotes.文档字符串必须用三个双引号定义。 No blank lines should be left before or after the docstring.在文档字符串之前或之后不应留下空行。 The text starts in the next line after the opening quotes .文本从左引号之后的下一行开始 The closing quotes have their own line (meaning that they are not at the end of the last sentence).结束引号有自己的一行(意味着它们不在最后一句话的末尾)。

Making a python script simultaneously adhere to pydocstyle and pycodestyle is a challenge.让 python 脚本同时遵守pydocstylepycodestyle是一个挑战。 But one thing which greatly helps is that in your docstring write the first line as summary of the function or class within 79 characters including .但有很大帮助的一件事是,在您的文档字符串中,将第一行写为 79 个字符内的函数或类的摘要,包括.

Then after leaving one blank line (for that using new line shortcut of your coditor is better than manually pressing enter ) you can write whatever you want and at that time focusing only on pycodestyle which is slightly easier than pydocstyle and the main reason is that our understanding of line and indentation is quite different than what system understands due to indentation settings, tab settings, line settings in the various code editors we use.So in this way you will have TODO from pycodestyle which you understand and can rectify instead of banging your head against the wall on pydocstyle TODO s.然后在留下一个空行之后(因为使用你的 coditor 的新行快捷方式比手动按enter更好)你可以写任何你想要的东西,当时只关注pycodestyle这比pydocstyle稍微容易一些,主要原因是我们的由于我们使用的各种代码编辑器中的缩进设置、制表符设置、行设置,对行和缩进的理解与系统理解的完全不同。因此,通过这种方式,您将拥有pycodestyle中的TODO ,您可以理解它并且可以纠正而不是敲打您的在pydocstyle TODO s 上pydocstyle

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

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