Python function argument list formatting

Question:

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.

Asked By: sam

||

Answers:

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)

Answered By: cwallenpoole