Matthias Arras Matthias Arras - 3 years ago 83
Python Question

Matching indentation level according to PEP8/flake8

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,

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,


def te(para1=x,

A solution where flake8 does not complain is to do:

def test(

However, when I am programming I don't necessarily know in advance how many parameters I'm gonna use in that
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?

ray ray
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download