假设我有两个变量
self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication
及
self.SuperLongSpecificCorperateVariableNameIcantChangeControl
我需要比较它们
问题是,当我将它们都放在if语句中时,它会超出样式检查器的行长度
if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication != self.SuperLongSpecificCorperateVariableNameIcantChangeControl):
解决这个问题的方法是把它分成两行
if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication \
!= self.SuperLongSpecificCorperateVariableNameIcantChangeControl):
我的同事们对PEP 8是否让您在条件句之间进行拆分,或者您是否可以拆分条件句本身存在分歧。理想情况下,我们可以获得更改变量名的批准,但与此同时,PEP8说在这种情况下我们应该怎么做
根据PEP8:
还有一个关于multiline if-statements的讨论,它没有采取明确的立场,但建议向条件的延续行添加缩进级别
最后,Should a Line Break Before or After a Binary Operator?建议在操作符之前中断(就像您所做的那样)。所以你可以写:
但是Python中的变量只是名称——一种引用对象的方式。名称可以更改。可以避免换行:
首先,PEP8说可以在Maximum Line Length下拆分长行:
事实上,由于括号的缘故,示例中不需要反斜杠
PEP8说可以在multiline if-statements下拆分条件,尽管该部分的主要重点是如何将其与下面的块区分开来
就个人而言,为了最大程度地提高可读性,我会选择最后一个选项。这就给了我们:
其他选择
您可以使用临时“内部使用”名称来缩短行:
这是假设上下文不在本地范围内。如果它实际上在本地范围内,则不需要是“内部使用”
可以使用helper函数在局部范围内为它们指定较短的名称。因为它们是属性,所以可以传入它们的对象:
相关问题 更多 >
编程相关推荐