Python function argument list formatting

PythonFormattingIndentationPep8

Python Problem Overview


What is the best way to format following piece of code accordingly to PEP8:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
    token=token, verifier=verifier, http_url=ACCESS_TOKEN_URL)

The problem is that if I place more than one parameter on the first line, the line exceeds 79 characters. If I place each of the parameters on a separate line with 4 spaces indentation it looks quite ugly:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(
    consumer,
    token=token,
    verifier=verifier,
    http_url=ACCESS_TOKEN_URL)

The best option that I come up with is to add extra indentation for better distinguishing:

oauth_request = oauth.OAuthRequest.from_consumer_and_token(
                        consumer,
                        token=token,
                        verifier=verifier,
                        http_url=ACCESS_TOKEN_URL)

I try to work out a general rule for me to use it for methods with long invocation on the first line and several parameters that can't fit a single line.

Python Solutions


Solution 1 - Python

My reading of the documentation suggests that 2 and 3 are both acceptable, but it looks like 2 is preferred (I say this because it looks like 2 vs. 3 is handled this way in the examples, I don't think that the style specification is very specific here). 1 is out (look at the docs under the line Arguments on first line forbidden when not using vertical alignment)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamView Question on Stackoverflow
Solution 1 - PythoncwallenpooleView Answer on Stackoverflow