1
votes

The following function is throwing an error no matter which way I write it.

@staticmethod
def _generate_clone_spec(param1=None,
                         param2=None,
                         param3=None,
                         param4=False,
                         param5=False):

According to PEP8 the code above is an acceptable way to align parameters in a function.

But the code show above throws the following error:

Indentation contains mixed spaces and tabs

If I convert all the spaces to tabs, then it looks like this:

@staticmethod
def _generate_clone_spec(param1=None,
                        param2=None,
                        param3=None,
                        param4=False,
                        param5=False):

In the code above, the mixed spaces and tabs error disappears, but then I violate a new error: Continuation line over-indented for visual indent (E127)

From what I can tell if i write the function in the following way, it conforms to both rules, but is there another way?

@staticmethod
def _generate_clone_spec(
    param1=None,
    param2=None,
    param3=None,
    param4=False,
    param5=False):
2
"If I convert all the spaces to tabs" - what? No! Other way. Stick to spaces.user2357112 supports Monica
I would put all params in one line.furas
@furas the parameters here are for demonstration purposes only. In reality the names are long enough that one line would cause that line to be longer than the recommended length.ARL
it is only recomendation, you don't have to respect it if you don't want it or it will make code more readable. But use spaces instead of tabs.furas
If there were more parameters the last example is how black would format it. Using the signature you currently have black would format it in a single line.John Keyes

2 Answers

1
votes

Answer

Use spaces. Then use the first example, but with only, and only spaces. Stop religiously following pep8. We don't have 80 character wide monitors anymore, nor is making code look a bit nicer such a horrible thing to do, especially when it's syntactically correct and whitespace in that case is irrelevant.

Reasoning

Disclaimer: I've come up with this, and essentially, this is my personal opinion.

I have never seen a better "rule" on deciding whether to use tabs or spaces in any kind of programming project but this:

If you format code by padding code with spaces, it objectively is the best decision to use only spaces no matter how you look at it.

Otherwise, it is whatever you prefer.

Not only does this avoid "mIxEd tAbS n sPaCeS" error in Python, it avoids confusion as to why pressing return on your keyboard makes the cursor jump erratically random amount of spaces.

If padded by spaces, and using spaces, you will always have... Spaces.

If you don't pad by spaces, and use tabs... You will always have return button jump back n spaces.

Everything else is insanity.

Forget pep8. Just be consistent in your own code, we could go on all day about this and never come up with how to indent properly.

0
votes

Align the code exactly as in your first example, but ensure that all your whitespaces consist of space characters only (i.e. no tab characters).

@staticmethod
def _generate_clone_spec(param1=None,
                         param2=None,
                         param3=None,
                         param4=False,
                         param5=False):

Note: most modern code editor applications offer the setting to automatically write multiple space characters (instead of a tab character) when pressing the tab key on your keyboard. This is a reliable way to avoid ending up with mixed spaces and tabs.