68

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.

sam
  • 1,211
  • 2
  • 13
  • 15

1 Answers1

46

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)

joon
  • 3,899
  • 1
  • 40
  • 53
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 6
    I disagree, I prefer 3. I like to make continuation indents considerably larger than normal block structure indents (4 spaces) in order to distinguish them. Normally I would line up continued call arguments with the first argument (following the opening paren). But in this case (since there is not enough space) the continuation lines should line up under the word 'oauth' following the equals sign. – Don O'Donnell Jul 07 '11 at 17:42
  • 1
    @Don Well, my reading of the documentation is that they're both OK. I just noticed that 2 is in a bunch of examples. Although, I think it is a different spacing from the standard 4 spaces. Truth be told, I have a strong preference to align all of the arguments with the called method's open paren, but that has more to do with what I'm used to than what is inherently "Pythonic". – cwallenpoole Jul 07 '11 at 17:46
  • 1
    I disagree as well. In the documentation that you have linked, both the first and second example provided in this post are clearly listed under the "No:" section, indicating that they should not be used. To clarify, each argument should not necessarily be supplied on its own line. However, when you must continue a statement across multiple lines, each line should have extra (i.e. double) indentation to distinguish it from child or sibling blocks. The last example is technically acceptable because of the extra indentation, but it would be preferred to use 8 space tabs for each extra line. – Corey O. Jun 18 '12 at 04:46
  • 3
    @CoreyO. I have to disagree with you. The PEP8 only forbids #2 when it is used in function definition. If you are calling an already-defined function, then #2 is perfectly fine. Both cases are demonstrated in PEP8. – RayLuo Jan 10 '16 at 08:49
  • 8
    I have a 20 year history of using style #2 and I can tell you that beyond a doubt it is the "less ugly" to my old eyes. :-) – moodboom Mar 05 '18 at 18:58
  • 1
    Or maybe I'm vertically-oriented, I'd rather scroll up and down more, than occasionally left and right. – moodboom Mar 05 '18 at 19:04