我知道我应该将 Python 代码保持在 79 列,但是如何缩进连续行呢?
我知道 Python 的标准行宽约定是 79 个字符。我知道行可以通过多种方式继续,例如自动字符串连接、括号和反斜杠。似乎没有明确定义的是溢出文本应该如何格式化。我要把它一直推回到第 1 栏吗?到原始行开始的那一栏?到括号的开头(如果适用)?例如,假设我有这样的内容:
self.someLongAttributeName = {'someLongKeyName':'someLongValueName',
'anotherLongKeyName':'anotherLongValueName'}
假设我上面使用的格式符合 79 个字符的限制,那么第二行的缩进是否正确?
现在假设如上所示的第一行是 > 79 个字符。在这种情况下,情况应该如何?
注意:我知道很多人不同意 79 个字符的约定。虽然我尊重这个问题的每一方都有很多优点和缺点,但这场辩论与我的问题无关。我问的是如何遵循惯例,而不是我是否应该遵循惯例,所以请不要在回复中宣扬放弃惯例的好处。谢谢。 =)
I am aware that the standard Python convention for line width is 79 characters. I know lines can be continued in a number of ways, such as automatic string concatenation, parentheses, and the backslash. What does not seem to be as clearly defined is how exactly the overflowing text should be formatted. Do I push it all the way back to col 1? To the col where the original line starts? To the start of the parentheses (if applicable)? For example, say I have something like this:
self.someLongAttributeName = {'someLongKeyName':'someLongValueName',
'anotherLongKeyName':'anotherLongValueName'}
Supposing that the format I used above would fit the 79 character limit, is the indentation of the second line correct?
Now suppose that the first line as shown above is > 79 characters. How should things look in that case?
NOTE: I know that a lot of people disagree with the 79-character convention. While I respect that there are a lot of pros and cons to each side of the issue, this debate is not relevant to my question. I am asking how to follow the convention, not whether or not I should, so please do not espouse the advantages of abandoning it in your reply. Thanks. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,这就是 PEP 8 在示例中的显示方式:
但是当左括号/大括号已经接近第 79 列时,我通常只是利用这一点:
并执行类似
或的操作
Yes, that's how PEP 8 shows it in examples:
But when the opening parenthesis/brace is already close to the 79th column, I usually just exploit this:
And do something like
or
http://www.python.org/dev/peps/pep-0008/< /a>
请参阅
最大行长度
http://www.python.org/dev/peps/pep-0008/
See
Maximum Line Length
即使在 Python 之外,我也一直对 C 代码执行此操作,这样我就可以(如 PEP 所说)在监视器上并排打开 2-3 个文件并查看所有文件。
反斜杠运算符
\
在 Python 和 C 中用作行继续运算符,但我更喜欢尝试使用括号()
或大括号{} 将行分组
(或者 Python 列表的方括号[]
),无论什么都是最方便的。如果我决定要在一个长连续块的中间添加另一行,我不想担心任何愚蠢的“陷阱”:缺少\
或更糟糕的是,在\
使延续无效。对于长条件语句,我喜欢双缩进,这样更明显的是该代码不是新块的一部分。
Even beyond Python, I do this all the time for my C code so I can (as the PEP says) have 2-3 files open on a monitor side-by-side and see them all.
The backslash operator
\
works as a line continuation operator in Python as well as C, but I prefer to try to group lines with parenthesis()
or braces{}
(or brackets[]
for Python lists), whatever is most convenient. If I decide I want to add another line in the middle of a long continuing block, I don't want to worry about any stupid 'gotchas': missing a\
or worse, some errant space after the\
invalidating the continuation.For long conditionals, I like to double-indent so it's more obvious that that code isn't part of the new block.
在PEP 8中有一个例子:
在我看来,这表明你的例子是正确的。我认为你应该在打开括号(
{
)后打破示例的第一行,如下所示:如果它太长。我不知道它是否是“pythonic”,但应该是熟悉和可读的。
In PEP 8 there is an example:
Which, to my eye, suggests that your example is correct. I think you should break first line of the example after opening bracket (
{
) like so:if it is too long. I don't know if it is 'pythonic', but should be familiar and readable.
我很确定这个问题之前已经在这里得到了回答,但我现在找不到它了。
简短的答案是 PEP8 不包括如何格式化对象文字,除了冒号之前应该有零个空格,之后有一个空格。
我这样做:
I was pretty sure this was answered here before, but I can't find it now so..
The short answer is that PEP8 does not cover how to format object literals, other than that colons should have zero spaces before and one space after.
I do them like this:
这样做的好技巧也是这样:
有些人喜欢在打开三重引号后使用 \,而不是过滤空行,但我真的很讨厌那些野兽。
对于你的具体例子,我同意cji。
Good trick is also to do like this:
Some people prefer to use \ after opening triple quote and not filter empty lines, but I really hate those beasts.
For your particular example I agree with cji.