这是一个在我的所有编程,python和其他方面都不断出现的问题.我真的很想把我的代码保持在80个字符以下,如果可能的话/不是非常难看的话.在像Perl这样的语言中,这并不太难,因为空白并不重要.在Python中,我最终会把头撞到墙上,而不是试图想出一种分裂我的长线的"好"方式.所以,Code大师,你是怎么做到的?您可以告诉我的任何一般策略?
我正在处理的一个特殊问题是:
self.SomeLongLongName = SomeLongLongName.SomeLongLongName(some_obj, self.user1, self.user2)
Run Code Online (Sandbox Code Playgroud)
当我自然而然地尝试用Python来解决这个问题时,对我来说唯一可行的方法似乎是:
self.SomeLongLongName = SomeLongLongName.SomeLongLongName(some_obj,
self.user1
self.user2)
Run Code Online (Sandbox Code Playgroud)
我想,它看起来并不那么糟糕,但它占用了三行,这完全没必要.必须有更好的方法,不是吗?
注意:我知道有些人不喜欢80个字符,并创建了自己的限制.我理解这背后的动机并尊重它,但80个字符是我的首选限制.请不要占用空间试图说服我去120或其他一些这里.
dav*_*idg 33
你的代码风格似乎坚持如果你在括号内打破一行,下面的行需要与它对齐:
self.SomeLongLongName = SomeLongLongName.SomeLongLongName(some_obj,
self.user1
self.user2)
Run Code Online (Sandbox Code Playgroud)
如果您愿意放弃此要求,可以按如下方式格式化代码,其中连续行具有固定的双缩进:
self.SomeLongLongName = SomeLongLongName.SomeLongLongName(
some_obj, self.user1, self.user2)
Run Code Online (Sandbox Code Playgroud)
这样可以避免在页面右侧边缘编写代码,并且一旦习惯就可以读取.它还有一个好处,如果您修改"SomeLongLongName"的名称,您不必重新缩进所有以下行.更长的例子如下:
if SomeLongLongName.SomeLongLongName(
some_obj, self.user1, self.user2):
foo()
else:
bar()
Run Code Online (Sandbox Code Playgroud)
连续行的双缩进允许您在视觉上将它们与缩进的行分开,因为它们位于块if
或else
块中.
正如其他人所指出的那样,使用短名称也有帮助,但这并不总是可行的(例如使用外部API时).
Joh*_*web 20
包装长行的首选方法是在括号,括号和括号内使用Python隐含的行继续.通过将表达式包装在括号中,可以在多行中分割长行.这些应该优先使用反斜杠来继续行.确保适当缩进续行.打破二元运算符的首选位置是运算符之后,而不是它之前.
PEP 8 Python代码样式指南(按照示例链接).
Mic*_*ent 15
self.SomeLongLongName = SomeLongLongName.\
SomeLongLongName(some_obj, self.user1, self.user2)
Run Code Online (Sandbox Code Playgroud)
'\' 是你的朋友.当然,您已经知道可以用逗号分隔参数列表中的行,而不使用'\'.另外,如果你有长串:
myLongString = "This is a really long string that is going to be longer than 80 characters so oh my what do I do to make this work out?"
Run Code Online (Sandbox Code Playgroud)
变为:
myLongString = "This is a really long string that is going to be longer than"\
" 80 characters so oh my what do I do to make this work out?"
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为Python将组合相邻的字符串文字,忽略相邻文字字符串之间的空格.
有些人引用Rectangle类作为一个糟糕的例子.pep8中的这个例子不是唯一的方法.
原版的:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
Run Code Online (Sandbox Code Playgroud)
这就是我写它的方式.
class Rectangle(Blob):
def __init__(self, width, height, color='black', emphasis=None,
highlight=0):
if (width == 0 and height == 0 and color == 'red' and
emphasis == 'strong' or highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
msg = "I don't think so -- values are %s, %s" % (width, height)
raise ValueError(msg)
Blob.__init__(self, width, height, color, emphasis, highlight)
Run Code Online (Sandbox Code Playgroud)
原因是:
%
自3.1以来不推荐使用). 归档时间: |
|
查看次数: |
22373 次 |
最近记录: |