Skip to content
Advertisement

PEP 8 – Aligning parameters by adding whitespace [closed]

I have a tendency to align my code by adding whitespace in the following (unconventional) way.

def my_demo_function(input1, input2, input3):
    print(input1 + input2 + input3)

my_demo_function("some long string", "some other string", "some final string")
my_demo_function("other string",     "another string",    "another other string")
my_demo_function("blah1",            "blah2 string",      "blah3" )

PEP-8 doesn’t seem to discuss this sort of (hack?) alignment. Are there any reasons I should not do this in my code? I can envision a situation where someone reading it doesn’t scroll to the right and misses something important.

Advertisement

Answer

This kind of aligning is usually avoided because of maintenance burden, especially when combined with version control systems, e.g., git.

For example, suppose you want to change some strings in your code later, e.g., "some long string" to "some very long string". Then your code becomes

my_demo_function("some very long string", "some other string", "some final string")
my_demo_function("other string",     "another string",    "another other string")
my_demo_function("blah1",            "blah2 string",      "blah3" )

if you do not change the other lines accordingly.

Now to keep aligning those, you would add more spaces to the second and the third lines, which could be bothersome (or not, depending on your text editor).

More importantly, suppose you are using git. The only “meaningful” change was done to the first line. But when you modify those two other lines, git would record that there was a change in those two lines as well, although this change is purely a cosmetic one. This might complicate things when you later review your git history.

That being said, it all depends on you. If you don’t use git, if you expect that the code is unlikely to be changed in the future, or if you don’t really care, then why not?

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement