Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP8 hanging indent specification

Tags:

python

pep8

PEP 8 regarding hanging indent:

When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.

Is there any explicit documentation about about "sub-arguments"? For example:

some_method(argument_one, argument_two, argument_three=[
    'parameter_one',
    'parameter_two',
    'parameter_three',
    ])

As opposed to:

some_method(
    argument_one,
    argument_two,
    argument_three=[
        'parameter_one',
        'parameter_two',
        'parameter_three',
        ]
    )

Preferably links to official discussions only.

like image 805
Ruslan Osipov Avatar asked Apr 03 '14 21:04

Ruslan Osipov


People also ask

What are PEP8 standards?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

What is hanging indented format?

A Hanging indent, also known as a second line indent, sets off the first line of a paragraph by positioning it at the margin, and then indenting each subsequent line of the paragraph.

Why do we use hanging indentation?

Hanging indents are used in the works cited or bibliography of MLA, APA, Chicago, and various other citation styles. They allow the reader to easily see the breaks between separate citations and quickly scan a works cited or bibliography for author names.

What is indentation in Python?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.


1 Answers

From PEP 8's "Other Recommendations" section:

Compound statements (multiple statements on the same line) are generally discouraged.

With this recommendation in mind, your 2nd example is probably more in line with the PEP 8 style guide, as it avoids compounding the method invocation and list construction on the same line. The 2nd example reads a little easier too.

like image 189
Alex Smith Avatar answered Oct 17 '22 09:10

Alex Smith