将long for语句缩短到最多79列的正确方法

2024-09-29 23:18:48 发布

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

我的问题是,制作以下代码行最传统的方法是什么:

self.steps = [setpoint_and_hold_time.setpoint for setpoint_and_hold_time in program.setpoints_and_hold_times]

到79个字符或更少的列?你知道吗

  • 根据PEP8,最大行长度应为79个字符。你知道吗
  • 很长一段时间以来,if语句我一直使用方括号,但我不确定在这里如何使用它们(如果这是解决方案的话)。你知道吗

另外,我假设这行代码不需要太多背景上下文,如果需要,请告诉我,我将在一些类中编辑解释这行代码。你知道吗

谢谢。你知道吗


Tags: and方法代码inselffortime传统
3条回答
self.steps = [setpoint_and_hold_time.setpoint
              for setpoint_and_hold_time
              in program.setpoints_and_hold_times]

/耸耸肩

由于循环变量的作用域有限,因此不需要给循环变量取长名称。我会缩短setpoint_and_hold_time。你知道吗

self.steps = [sh.setpoint for sh in program.setpoints_and_hold_times]

black将重新格式化为(符合PEP8,我认为它非常可读):

self.steps = [
    setpoint_and_hold_time.setpoint
    for setpoint_and_hold_time in program.setpoints_and_hold_times
]

相关问题 更多 >

    热门问题