Python PEP: blank line after function definition?

bgusach picture bgusach · Sep 20, 2013 · Viewed 20.6k times · Source

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.

Answer

moliware picture moliware · Sep 20, 2013

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'