Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PEP: blank line after function definition?

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.

like image 970
bgusach Avatar asked Sep 20 '13 10:09

bgusach


People also ask

How many blank lines should be before and after method definitions inside a class method definitions inside a class are just another term for functions in a class?

Surround top-level function and class definitions with two blank lines. Method definitions inside a class are surrounded by a single blank line. Extra blank lines may be used (sparingly) to separate groups of related functions.

How do you insert a blank line in Python?

The new line character in Python is \n .

How many blank lines does the PEP8 Standard recommend to use between top level function definitions within a Python module?

PEP8 says you have to surround top level functions with 2 lines, however if you were to have a constant/global variable there, instead of those functions it could have easily been 1 line.

Is blank line allowed in Python?

Blank line is definitely allowed in python functions. All of yours examples (A,B,C,D) should work and the problem probably (sure) is related to "Eclipse and the PyDev plug-in".


3 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' 
like image 198
moliware Avatar answered Sep 25 '22 23:09

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.

like image 25
Ryan de Kleer Avatar answered Sep 22 '22 23:09

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

like image 20
timgeb Avatar answered Sep 23 '22 23:09

timgeb