Python 编码约定“块前连续缩进错误:由 pylint 发现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27466303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python coding convention "Wrong continued indentation before block: found by pylint
提问by Reed_Xia
I used pylint to check my python code, and found this convention problem:
我用pylint检查了我的python代码,发现了这个约定问题:
C:11, 0: Wrong continued indentation before block.
+ this_time <= self.max):
^ | (bad-continuation)
I tried to refine for times but the problem is still present, can someone help? Thanks!
我尝试了多次优化,但问题仍然存在,有人可以帮忙吗?谢谢!
if len(remaining_obj_list) > 0:
for i in a_list:
this_time = self.__get_time(i)
for remaining_obj in remaining_obj_list:
if (remaining_obj.get_time() # to fit 78 char rule
+ this_time <= self.max):
i.append(remaining_obj)
remaining_obj.set_used(True)
if 0 == len(self.__get_unused_list):
break
采纳答案by sthenault
Pylint doesn't want such continuation to start on the same column as the next indentation block. Also, notice that the message includes a hint on columns that it considers correct.
Pylint 不希望这样的延续与下一个缩进块在同一列上开始。另外,请注意该消息包含有关它认为正确的列的提示。
回答by Amber
Try putting the +
on the previous line:
尝试将+
放在上一行:
if (remaining_obj.get_time() +
this_time <= self.max):
As a side note though, you might want to consider the factors that are causing your code to have to fit within ~40 characters - perhaps you have a few too many indentation levels and your code could be refactored to have fewer nested blocks.
不过,作为旁注,您可能需要考虑导致您的代码必须适应约 40 个字符的因素 - 也许您有太多的缩进级别,并且您的代码可以重构为具有更少的嵌套块。
回答by feasel0
回答by sthenault
Check for spurious tabs (in Sublime: Ctrl + F, then enter a single space) and replace them by the correct number of spaces. I had the same issue and while PyLint was complaining about line continuation, the error was actually triggered by misplaced tabs.
检查虚假标签(在 Sublime 中:Ctrl + F,然后输入一个空格)并用正确数量的空格替换它们。我遇到了同样的问题,虽然 PyLint 抱怨行继续,但错误实际上是由放错位置的选项卡触发的。
At indentations, PyLint seems to count spaces only and throws this error if the numbers don't add up to multiples of 4. Depending on the editor, misplaced tabs may not be immediately visible.
在缩进时,PyLint 似乎只计算空格,如果数字加起来不是 4 的倍数,则会抛出此错误。根据编辑器的不同,错位的选项卡可能不会立即可见。