-2

The question is how to properly break lines according to PEP8 while using TABs.

So here is a related question How to break a line in a function definition in Python according to PEP8. But the issue is that this only works properly when the length of the definition header def dummy( is an integer multiple of the tab length.

def tes(para1=x,
--->--->para2=y)

Otherwise I end up with a new error and flake8 complains about Error E127 or E128 because the its either over- or under-indented like this:

Under-indented E128

def test(para1=x,
--->--->para2=y)

Over-indented

 def te(para1=x,
 --->--->para2=y)

A solution where flake8 does not complain is to do:

def test(
--->--->para1=x,
--->--->para2=y
--->--->)

However, when I am programming I don't necessarily know in advance how many parameters I'm gonna use in that test() function. So once I hit the line limit I have rearrange quite a bit.

This obviously does apply to all continuations. Does this mean the cleanest solution is to break the line as soon as possible for every line which final length cannot be said by the time of first writing, or is there another solution.

Tab and space shall not be mixed for the solution.

So now I ask myself what is the legis artis to deal with line continuations?

martineau
  • 119,623
  • 25
  • 170
  • 301
Matthias Arras
  • 565
  • 7
  • 25
  • 1
    PEP-0008, under the [Tabs or Spaces?](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) section, has a clear bias towards using spaces. Why not convert from tabs to use only spaces as suggested? – code_dredd Jul 25 '17 at 23:07
  • You should ask a clear question. – Difster Jul 25 '17 at 23:11
  • Definitely use spaces and adjust the indent of following lines so that it matches the position of the first parameter in the definition line. – a_guest Jul 25 '17 at 23:13
  • 1
    So I go with 4 spaces instead of a tab and in those instances I just add a space? That being said, the answer is: there is no good way doing this with TABs, use spaces instead? If @ray or anyone else wants elaborate a bit on it, I'll gladly accept it as an answer. – Matthias Arras Jul 25 '17 at 23:20
  • @MatthiasArras Replace each tab character by 4 spaces in the whole file/module. I'm not sure which part of this might be confusing. Can you clarify? – code_dredd Jul 25 '17 at 23:23
  • @ray everything crystal clear and helpful, I just though you may wanted to put an official answer down so I could accept, that's all. Thanks. – Matthias Arras Jul 25 '17 at 23:26

2 Answers2

1

I'm turning my original comment into an official answer.


The PEP-0008 has a section about whether to use Tabs or Spaces, quoted below (with my emphasis):

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python 3 disallows mixing the use of tabs and spaces for indentation.

Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

You're running into issues with tabs, and you don't say whether you're using Python2 or 3, but I'd suggest you stick to the PEP-0008 guidelines.

You should replace tab chars in the file/module with 4 spaces and use spaces exclusively when indenting.


WARNING: Be very careful if you plan to use shell commands to do this for you, as some commands can be dangerous and mangle intended tab chars within strings (i.e. not only indentation tabs) and can break other things, such as repositories -especially if the command is recursive.

code_dredd
  • 5,915
  • 1
  • 25
  • 53
0

PEP8 is very clear:

Tabs or Spaces?

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python 3 disallows [emphasis added] mixing the use of tabs and spaces for indentation.

Reference: python.org.

So if you're writing new code and want to adhere to standards, just use spaces.

Community
  • 1
  • 1
TomServo
  • 7,248
  • 5
  • 30
  • 47