根据 PEP8/flake8 匹配缩进级别

Matching indentation level according to PEP8/flake8

问题是如何在使用 TAB 时根据 PEP8 正确换行。

所以这是一个相关的问题。但问题是,这仅在定义 header def dummy( 的长度是制表符长度的整数倍时才能正常工作。

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

否则我会得到一个新的错误并且 flake8 抱怨错误 E127 或 E128 因为它要么超过要么 under-indented 像这样:

Under-indented E128

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

Over-indented

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

flake8 不抱怨的解决方案是:

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

然而,当我编程时,我不一定事先知道我将在 test() 函数中使用多少参数。因此,一旦达到行数限制,我就会重新排列很多。

这显然适用于所有延续。这是否意味着最干净的解决方案是在第一次写入时不能说出最终长度的每一行尽快换行,或者是否有其他解决方案。

Tab 和 space 不得混合用于解决方案。

所以现在我问自己什么是 legis artis 来处理续行?

PEP8 很清楚:

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.

参考:python.org.

因此,如果您正在编写新代码并希望遵守标准,请使用空格。

我正在把我原来的评论变成一个正式的回答。


PEP-0008 有一个关于是否使用 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 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!

您 运行 遇到了制表符问题,您没有说明您使用的是 Python2 还是 3,但我建议您坚持 PEP-0008 准则.

您应该将 file/module 中的制表符替换为 4 个空格,并在缩进时专门使用空格。


警告: 如果您打算使用 shell 命令为您执行此操作,请务必小心,因为某些命令可能很危险并且会破坏字符串中的预期制表符(即不仅仅是缩进选项卡)并且可以破坏其他东西,例如存储库 - 特别是如果命令是递归的。