PEP8:为视觉索引而缩进的续行

2024-05-11 23:04:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一行代码,在测试pep8错误时,我得到: 排队太长。所以为了尝试修复这个问题,我使用了斜线('\'),但是为了视觉上的缩进,我得到了缩进上的续行。我能做什么来解决这个问题?

enter image description here

我尝试过的事情:

if first_index < 0 or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 \ 
   or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 or \
   second_index > self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:
     raise ValueError

continuation line over-indented for visual indent

Tags: orofselfnumberindexiflineover
1条回答
网友
1楼 · 发布于 2024-05-11 23:04:12

行扩展的反斜杠有一个问题,即有可能破坏代码的尾随空格。这是一个流行的解决方案,符合PEP8:

if (first_index < 0 or
    second_index > self._number_of_plates - 1):

相关问题 更多 >