简体   繁体   English

如何在Python代码中包装和对齐注释?

[英]How to wrap and align comments in Python code?

I'm trying to make my Python code look more readable. 我试图使我的Python代码看起来更具可读性。 I read the style guide but I don't know how to get something like this 我阅读了风格指南,但我不知道如何得到这样的东西

x = foo(x);  # compute the value of the next prime number
             # that is larger than x  (foo is a really bad 
             # choice for this function's name) 

Or this 或这个

x = x + 1                 # Compensate for border
some other code           # some other comment

How do you wrap the comment and align them like this? 你如何包装评论并将它们对齐? You don't just type a bunch of space , do you? 你不只是输入一堆space ,对吗? What if I edited the code, do I have to realign the comments manually? 如果我编辑了代码,我是否必须手动重新排列注释?

I'm using emacs as my editor. 我正在使用emacs作为我的编辑器。

I don't think you want this at all. 我认为你根本不想要这个。 Lattyware already explained the second case, but let's look at the first: Lattyware已经解释了第二种情况,但让我们来看看第一种情况:

x = foo(x);  # compute the value of the next prime number
             # that is larger than x  (foo is a really bad 
             # choice for this function's name) 

Comments that are too long to fit in-line can be turned into block comments above the code, like this: 太长而无法嵌入的注释可以转换为代码上方的块注释,如下所示:

# compute the value of the next prime number that is larger than
# x (foo is a really bad choice for this function's name)
x = foo(x);

That seems more readable than the right-aligned comments. 这似乎比右对齐的评论更具可读性。 It also gives you more room. 它还为您提供更多空间。 And it's definitely easier with emacs (just type the whole thing and meta-Q it). 使用emacs肯定更容易(只需键入整个内容和meta-Q)。 And, quoting Inline Comments in PEP 8: 并引用PEP 8中的内联评论

Use inline comments sparingly. 谨慎使用内联评论。

An inline comment is a comment on the same line as a statement. 内联注释是与语句在同一行上的注释。

This is the very beginning of the style guide for inline comments, and it implies pretty strongly that if you're trying to write more than you can fit on the same line, you should be using a block comment instead. 这是内联注释的样式指南的开始,它强烈地暗示如果你想要写的数量超过你可以放在同一行上,你应该使用块注释。

Also, while we're talking about PEP 8: 另外,在我们谈论PEP 8时:

  • "Comments should be complete sentences." “评论应该是完整的句子。” Your first comment needs periods. 您的第一条评论需要期间。 (Yes, it also says "If a comment is short, the period at the end can be omitted", but you have a 3-line 2-sentence comment, so that doesn't apply here.) (是的,它也说“如果评论很短,最后的句号可以省略”,但你有一个3行2句的评论,所以这里不适用。)
  • " If a comment is a phrase or sentence, its first word should be capitalized". “如果评论是短语或句子,则其第一个词应该大写”。 So, capitalize "Compute" (but not "foo", because that's an identifier). 所以,大写“Compute”(但不是“foo”,因为那是一个标识符)。
  • Don't add a comment that the function's name is bad, just rename the function. 不要添加函数名称不好的注释,只需重命名该函数即可。
  • Get rid of that semicolon. 摆脱那个分号。

So: 所以:

# Compute the value of the next prime number that is larger than x.
x = next_larger_prime(x)

But once you've done that, you don't even need the comment. 但是一旦你这样做了,你甚至不需要评论。

And in fact, that's pretty common. 事实上,这很常见。 When you find yourself wondering how to break the style guidelines on commenting, you should probably instead by asking how to reorganize the code so it doesn't need all those comments. 当您发现自己想知道如何打破评论的样式指南时,您可能应该通过询问如何重新组织代码以使其不需要所有这些注释。 It's not always possible, but it's usually worth at least trying. 这并不总是可能,但通常值得至少尝试。

I would argue that the two cases are vastly different. 我认为这两种情况大不相同。 In the first case, I would use spaces over tabs, as you want the comments to align regardless to tab width setting in the editor of the user. 在第一种情况下,我会在选项卡上使用空格,因为您希望注释无论用户编辑器中的选项卡宽度设置如何都要对齐。 Obviously, if you use tabs for indentation of code normally, use tabs until you reach the level of the code, then spaces. 显然,如果您通常使用制表符来缩进代码,请使用制表符直到达到代码级别,然后使用空格。

Imagine using tabs: 想象一下使用标签:

x = foo(x) # compute the value of the next prime number
⟶⟶⟶⟶ # that is larger than x  (foo is a really bad 
⟶⟶⟶⟶ # choice for this function's name) 

Now imagine someone uses a shorter setting for tab length: 现在想象有人使用较短的设置选项卡长度:

x = foo(x) # compute the value of the next prime number
→→→→ # that is larger than x  (foo is a really bad 
→→→→ # choice for this function's name) 

I would argue, however, you might want to replace this with a triple quoted string instead: 不过,我认为,您可能希望用三引号字符串替换它:

"""Compute the value of the next prime number
that is larger than x  (foo is a really bad 
choice for this function's name)."""
x = foo(x)

In the second case, I don't think aligning the comments adds to readability, I would just put them at the end of the line. 在第二种情况下,我不认为对齐注释会增加可读性,我只是将它们放在行尾。 PEP-8 recommends against aligning assignments, dict literals, etc... - and I would consider this an extension of that. PEP-8建议不要对齐作业,词典文字等...... - 我认为这是对此的延伸。

x = x + 1 # Compensate for border
some other code # some other comment

You should not do this. 你不应该这样做。 The first should be formatted like this: 第一个应格式如下:

# Compute the value of the next prime number that is larger than x
# (foo is a really bad choice for this function's name).
x = foo(x)

And the second: 第二个:

x = x + 1  # Compensate for border
some other code   # some other comment

Nonwithstanding the reasons to use alternatives, if you ever find yourself in a position where you need this, say for commenting the lines of a proof written in pseudocode-Python, you can use align-regexp : 尽管使用替代方案的原因是,如果您发现自己处于需要此位置的位置,比如评论用伪代码Python编写的证明的行,您可以使用align-regexp

M-x align-regexp RET  # RET

(put a space before the # sign) should do what you asked for. (在#符号前放置一个空格)应该按照你的要求做。 It works on the currently selected region. 它适用于当前选定的区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM