是否可以使用带列缩进的 PEP8

Is it possible to use PEP8 with column indentation

我经常有一些代码,出于可读性的原因,我想在按列结构中缩进。例如:

props = {
    'name'    : foo(df, 'name'),
    'address' : foo(df, 'address'),
    'phone'   : foo(df, 'phone'),
    'surname' : foo(df, 'surname'),
    'age'     : foo(df, 'age'),
    'height'  : foo(df, 'height'),
    'weight'  : foo(df, 'weight'),
    ...
}

由于额外的空格,这当然会导致 PEP8 警告,这会损害我们的样式检查器和格式化程序。

有没有办法让列结构和PEP8和平共处?

您可以在 PEP8 配置中disable a particular rule

[pycodestyle]
count = False
ignore = E226,E302,E41  <-------------- here I am!
max-line-length = 160
statistics = True

Here 是要忽略的错误代码。


如果您不想在任何地方禁用规则,您可以在您不想被检查的行的末尾添加 #noqa 注释。该行中的所有 PEP8 错误都将被禁用。

如果您使用多个 linter,您也应该检查并re-config它们。

您可以在冒号后添加多余的空格,如下所示:

props = {
    'name':     foo(df, 'name'),
    'address':  foo(df, 'address'),
    'phone':    foo(df, 'phone'),
    'surname':  foo(df, 'surname'),
    'age':      foo(df, 'age'),
    'height':   foo(df, 'height'),
    'weight':   foo(df, 'weight'),
    ...
}