Reputation: 1026
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):
Upvotes: 1
Views: 3090
Reputation:
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.
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.
Upvotes: 3
Reputation: 8634
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.
Upvotes: 0