28

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.

bgusach
  • 13,019
  • 10
  • 44
  • 61
  • I read it as "don't you do no friggin' space-wasting empty lines not, unless one of these rare exceptions apply". Come on – does it really increase readability to separate the `def` from the code? – Alfe Sep 20 '13 at 10:50
  • I definitely read that as being an exhaustive list of all the places you can put vertical whitespace. Saying that, I would put a single blank line between imports based on where they come from. Grouping core python libs, 3rd party libs, and homegrown libs together. In that order. – Tom Manterfield Jan 02 '14 at 14:39

3 Answers3

39

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'
moliware
  • 9,478
  • 3
  • 32
  • 46
  • 3
    I can't find anything in the doc that says you *must* write a docstring, even for really obvious functions. I'm a big fan of docstrings, but if the docstring cannot add anything to the readability, I would omit it altogether. I've seen this before: ``` def set_main_window_icon(self, icon_path): """Set main window icon.""" ``` and that is a waste of lines. I get that hello_function is a contrived invention, but I worry about encouraging that sort of docstring by example. – Ryan de Kleer Nov 30 '18 at 17:19
26

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.

Ryan de Kleer
  • 908
  • 11
  • 20
1

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

timgeb
  • 64,821
  • 18
  • 95
  • 124