Home > Software design >  How to indent correctly multi-line list of arguments inside function call in Python [closed]
How to indent correctly multi-line list of arguments inside function call in Python [closed]

Time:09-29

I want to create this multi-line function call. The examples that I find for nested constructs they assume that there is a character starting each line i Cannot find an example where a bracket [ is followed by a parenthesis ( without anything in between.

So how would I format this:

  subprocess.run(['docker-compose', '-f', 'docker-compose.test.yml', '-f',
                   'docker-compose.services.yml', '-f', 'docker-compose.dev.yml', 'down'],
                 stdout=subprocess.DEVNULL
  )

where should the bracket go? How should i format the list of arguments? Is it ok if I have multiple on the same line? Or should each argument has its own line

CodePudding user response:

You can do something like this

import subprocess

subprocess.run(
    [
        'docker-compose', '-f', 'docker-compose.test.yml', '-f',
        'docker-compose.services.yml', '-f', 'docker-compose.dev.yml', 'down'
    ],
    stdout=subprocess.DEVNULL
)

CodePudding user response:

I personally like to use black to answer questions of formatting. It's more strict that PEP8 and makes some hard decisions I don't always agree with, but I've actually started using it systematically, it really saves a lot of wasted time worrying about formatting.

Here's what black did with this code:

subprocess.run(
    [
        "docker-compose",
        "-f",
        "docker-compose.test.yml",
        "-f",
        "docker-compose.services.yml",
        "-f",
        "docker-compose.dev.yml",
        "down",
    ],
    stdout=subprocess.DEVNULL,
)

I have to say if I was formatting this by hand, I would prefer keeping switches and their values on the same line, so I would probably do this:

subprocess.run(
    [
        "docker-compose",
        "-f", "docker-compose.test.yml",
        "-f", "docker-compose.services.yml",
        "-f", "docker-compose.dev.yml",
        "down",
    ],
    stdout=subprocess.DEVNULL,
)

And I also think prnvbn's answer is just as good.

Now, PEP8 is not actually strict about all this, I think it accepts several options on placing the closing ] and ), and it also seems to be flexible on whether the first element appears on the same line as what contains it or not. While I don't like your formatting much, it might actually still be PEP8 compliant, except for the fact that your second line is longer than 79 characters.

CodePudding user response:

As other have said, PEP8 gives several options with respect to this and, between those, whichever you choose is matter of personal preference. Personally, I like my code lines to be compact with a number of white lines in between to demark whenever I'm doing something different (e.g., initializing variables vs. computing something). In this case, I would go with (i.e., my personal preference):

  subprocess.run(['docker-compose', '-f',
                  'docker-compose.test.yml', '-f',
                  'docker-compose.services.yml', '-f',
                  'docker-compose.dev.yml', 'down'],
                 stdout=subprocess.DEVNULL)

Note however that if you are coding this in the context of a project or while working on code started by others, you generally want to follow the preferences and style that the code exhibits already.

  • Related