Python PEP: blank line after function definition?

Question:

I can’t find any PEP reference to this detail. 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.

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 (e.g. a set of dummy implementations).

Use blank lines in functions, sparingly, to indicate logical sections.

Python accepts the control-L (i.e. ^L) form feed character as
whitespace; 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.

Asked By: bgusach

||

Answers:

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'
Answered By: moliware

As pointed out by @moliware, the Docstring Conventions state, under One-line Docstrings:

There’s no blank line either before or after the docstring.

HOWEVER, it also says (under 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.

Answered By: Ryan de Kleer

Projects use different docstring conventions.

For example, the pandas docstring guide explicitly requires you to put triple quotes into a line of their own.

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

Answered By: timgeb

Making a python script simultaneously adhere to pydocstyle and pycodestyle is a challenge. 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 ..This way you adhere to both PEP 257 (as per pydocstyle) of having a period at the end of an unbroken line and 79 characters limit of PEP 8 (as per pycodestyle).

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

Answered By: riskdoctor
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.