Python 中这句话的含义是什么?

发布于 2024-12-22 21:47:30 字数 595 浏览 1 评论 0 原文

更具体地说,我不确定紧随其后的“%”和“\”符号的含义:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
    (self.id, a, b, c, d, e, f)

此 return 语句是否被视为一行代码?因为我最初认为“\”是为了将看起来过长的行切成两段,以增强可读性并避免环绕。

另外,我这里的表格反映了我的代码中的内容。 '(self.id, a, b, c, d, e, f)' 部分确实比 return 语句的开头更远。

基本上,该语句是否相当于:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % (self.id, a, b, c, d, e, f)

在这个语句中不涉及“\”......

More specifically, I'm not sure what the "%" and "\" symbols coming right after each other are supposed to mean:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
    (self.id, a, b, c, d, e, f)

Is this return statement considered to be one line of code? Because I originally thought the "\" was there to chop up what seems to be an excessively long line into two pieces to enhance readability and to avoid wrap-around.

Also, I the tabulation here reflects what is in my code. The part that says '(self.id, a, b, c, d, e, f)' is indeed tabbed once further than the beginning of the return statement.

Basically, is the statement equivalent to:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % (self.id, a, b, c, d, e, f)

in this one there is no "\" involved....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞取 2024-12-29 21:47:30

% 符号称为字符串格式化字符串插值运算符,在 字符串格式化操作。另请注意,在我的示例中,我将 % 符号移至下一行,而不是将其保留在上一行的末尾。两种形式都是有效的,但是将其与用于插值的数据放在同一行往往会使阅读更清晰且更易于维护。

是的,这两段代码是等效的,因为反斜杠将两个或多个物理行视为单个逻辑行,如 Python 文档中有关 显式行连接。但是,我会避免使用显式行连接,而是使用第三种等效方法,称为 隐式行连接

return ('guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }'
        % (self.id, a, b, c, d, e, f))

因为它在重新格式化时不太容易出错,并且还允许您使用注释,例如在这个重新格式化的示例中:

return ('guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' 
        % (self.id, 
           a, 
           b, 
           # c1, 
           c2, 
           d, 
           e, 
           f))

尝试使用反斜杠做同样的事情...不仅更困难,而且您也不能使用评论。

请注意,返回值周围的括号并非多余,正如某些人所建议的那样;它们允许在上下文中使用隐式行连接变量插值(与 % 后面的元组分开,它是隐式行连接也发生的附加上下文):

括号、方括号或花括号中的表达式可以是
不使用反斜杠拆分为多个物理行。

总之...请尽可能避免使用反斜杠显式连接行! (考虑到替代方案,几乎永远没有必要)。

与显式/隐式行连接相关的主题是使用字符串文字。考虑到吉他示例的 C 风格块语法,我希望将返回值重新格式化为多行,如下所示:

class c(object):
    def f(self):
        return """
guitar {
  id: %d, 
  relevant_properties: (%.02f, %.02f, %.02f),
  irrelevant_properties: (%.02f, %.02f, %.02f)
}
""" % (self.id, a, b, c, d, e, f)

我已将此示例放在类方法定义的更完整上下文中,因此会更清楚什么格式将如下所示;多行字符串将与缓冲区的左侧齐平,并且任何空格都将逐字显示在输出中。

但请注意,上面的格式可能会引入不需要的前导或尾随换行符,因此这是我偶尔建议使用显式行连接的少数情况之一。这里的原因是消除不需要的额外换行符,作为三引号字符串为我们提供的增强代码可读性的权衡,因为它允许我们或多或少地以我们在最终输出中看到的方式看到完整的节;与上面的比较,您会看到在第一次出现 """ 的末尾仅添加了一个 \ 字符:

class c(object):
    def f(self):
        return """\
guitar {
  id: %d, 
  relevant_properties: (%.02f, %.02f, %.02f),
  irrelevant_properties: (%.02f, %.02f, %.02f)
}
""" % (self.id, a, b, c, d, e, f)

The % symbol is known as the string formatting or string interpolation operator and is described in String Formatting Operations. Also, note that in my examples I moved the % symbol to the next line instead of leaving it on the end of the previous line. Both forms are valid, however putting it on the same line as the data used for interpolation tends to make it clearer to read and easier to maintain.

Yes, the two pieces of code are equivalent since the backslash treats two or more physical lines as a single logical line as described in the Python documentation about the \ symbol in the section about Explicit Line Joining. However I would avoid using explicit line joining and instead use this third equivalent way called Implicit Line Joining:

return ('guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }'
        % (self.id, a, b, c, d, e, f))

Because it is less prone to errors when reformatting and also allows you to use comments, such as in this reformatted example:

return ('guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' 
        % (self.id, 
           a, 
           b, 
           # c1, 
           c2, 
           d, 
           e, 
           f))

Try doing that same thing with backslashes... not only is it more arduous, but you also cannot use comments.

Note that the parentheses around the return value are not superfluous as some have suggested; they are what allows the use implicit line joining in the context of the variable interpolation (which is separate from the tuple following the % which is an additional context where implicit line joining also happens):

Expressions in parentheses, square brackets or curly braces can be
split over more than one physical line without using backslashes.

In summary... please avoid explicit line joining with backslashes wherever possible! (It is almost never necessary given the alternatives).

A related subject to explicit/implicit line joining is the use of triple-quoted strings discussed in the section on String Literals. Given the C-style block syntax of your guitar example, I expect it would probably be valid to reformat your return value into multiple lines like this:

class c(object):
    def f(self):
        return """
guitar {
  id: %d, 
  relevant_properties: (%.02f, %.02f, %.02f),
  irrelevant_properties: (%.02f, %.02f, %.02f)
}
""" % (self.id, a, b, c, d, e, f)

I have put this example in the fuller context of a class method definition so it will be clearer what the formatting will look like; the multi-line string will be flush against the left side of the buffer and any spaces will appear verbatim in the output.

Be aware, however, the formatting above may introduce unwanted leading or trailing newlines, so this is one of the few cases where I occasionally recommend the use of explicit line joining. The reason here is to eliminate unwanted extra newline characters as a trade-off for the enhanced code-readability that triple-quoted strings gives us since it allows us to see the complete stanza more or less the way we would see it in the final output; comparing above you'll see just the addition of a single \ character at the end of the first occurrence of the """:

class c(object):
    def f(self):
        return """\
guitar {
  id: %d, 
  relevant_properties: (%.02f, %.02f, %.02f),
  irrelevant_properties: (%.02f, %.02f, %.02f)
}
""" % (self.id, a, b, c, d, e, f)
马蹄踏│碎落叶 2024-12-29 21:47:30

是的,行尾的 \ 转义换行符,因此这两段代码是等效的。

此处描述: http://docs.python.org/reference/ lexical_analysis.html#explicit-line-joining

Yes, the \ at the end of a line escapes the newline so those two pieces of code are equivalent.

It is described here: http://docs.python.org/reference/lexical_analysis.html#explicit-line-joining

爱的那么颓废 2024-12-29 21:47:30

是的,您拥有的是一个分成两行的简单字符串格式。反斜杠运算符仅允许您将 python 语句分成多行(因为 Python 对空格敏感)。

Yes, what you have there is a simple string format broken across two lines. The backslash operator simply allows you to break python statements on to multiple lines (since Python is whitespace sensitive).

世界和平 2024-12-29 21:47:30

反斜杠字符使 python 忽略第一行末尾的换行符(参见 文档)。所以是的,你的两段代码确实是等价的。

The backslash character makes python ignore the newline at the end of the first line (cf. the docs). So yes, your two pieces of code are indeed equivalent.

握住我的手 2024-12-29 21:47:30

因为我最初认为“\”是为了将看起来过长的行分成两部分,以增强可读性并避免环绕。

这是。你觉得什么令人困惑?

将行连接起来后,您将得到一个长字符串,后跟一个 % ,然后是一个元组。这具有通常的含义; % 用于字符串插值。

Because I originally thought the "\" was there to chop up what seems to be an excessively long line into two pieces to enhance readability and to avoid wrap-around.

It is. What do you find confusing?

After the line is joined up, you just get a long string followed by a % followed by a tuple. This has the usual meaning; % is used for string interpolation.

佞臣 2024-12-29 21:47:30

在使用 IDLE 打开的脚本中,我在以下函数体中获得了以下缩进:

def f(self, a, b, c, d, e, f):
    return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
           (self.id, a, b, c, d, e, f)

要获得此缩进,请将光标放在 (%.02f, %.02f, %.02f) }' % \(self.id, a, b, c, d, e, f) ,然后单击 ENTER

如果我复制,这里就只有上面的两行函数的主体,无需单击 {} 按钮,我得到:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
       (self.id, a, b, c, d, e, f)

aculich 答案中的缩进是正确的,并且您问题中的缩进并不是真的不正确(因为您的代码可以正常工作而不会出错),但是视觉上不愉快。

就我个人而言,我认为最好将操作符号 % 放在 \ 后面的行上,而不是放在它之前,例如:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }'\
       % (self.id, a, b, c, d, e, f)

人们可以改进显示,从而减少宽度,就像这样:

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}') \
        % (self.id, a, b, c, d, e, f)

Bouaif(这是一种试图表达怀疑的个人法语拟声词),这并不美妙,因为它混合了隐式和显式的行连接。

编辑:然后,考虑到 aculich 的解释,上面的宽度减小的解决方案也可以写成

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}'
        % (self.id, a, b, c, d, e, f) )

好吧,这有更多的风格。

但我犹豫不知道在后一种和以下方式中我更喜欢哪一种
当我思考这一切时,我想到了这一点:

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}'
        ) % (self.id, a, b, c, d, e, f)

In a script, opened with IDLE, I obtained the following indentation in the following function's body:

def f(self, a, b, c, d, e, f):
    return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
           (self.id, a, b, c, d, e, f)

To obtain this indentation, place the cursor between (%.02f, %.02f, %.02f) }' % \ and (self.id, a, b, c, d, e, f) , then click on ENTER

If I copy, here on SO, only the two lines of the above function's body , without clicking on {} button, I obtain:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }' % \
       (self.id, a, b, c, d, e, f)

The indentation in aculich's answer is correct , and the one in your question is not really incorrect (because your code will work without giving an error) but not visually pleasant.

.

Personally, I think it's preferable to put the operating sign % on the line following \ rather than before it, like that:

return 'guitar { id: %d, relevant_properties: (%.02f, %.02f, %.02f), irrelevant_properties: (%.02f, %.02f, %.02f) }'\
       % (self.id, a, b, c, d, e, f)

.

One can improve the display so that the width is reduced, like that:

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}') \
        % (self.id, a, b, c, d, e, f)

Bouaif (that's personal french onomatopoeia trying to express doubt), that's not fantastic, because it mixes implicit and explicit line joining.

EDIT: then, taking account of aculich's explanations, the above reduced-width solution can also be written

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}'
        % (self.id, a, b, c, d, e, f) )

Well, that has got more style.

But I hesitate in knowing which one I prefer among this latter one and the following manner
that came to my mind while thinking about all that:

return ('guitar '
        '{ '
        'id: %d, '
        'relevant_properties: (%.02f, %.02f, %.02f), '
        'irrelevant_properties: (%.02f, %.02f, %.02f) '
        '}'
        ) % (self.id, a, b, c, d, e, f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文