• <small id='9j9eF'></small><noframes id='9j9eF'>

      <bdo id='9j9eF'></bdo><ul id='9j9eF'></ul>
    <tfoot id='9j9eF'></tfoot>
    <legend id='9j9eF'><style id='9j9eF'><dir id='9j9eF'><q id='9j9eF'></q></dir></style></legend>
        <i id='9j9eF'><tr id='9j9eF'><dt id='9j9eF'><q id='9j9eF'><span id='9j9eF'><b id='9j9eF'><form id='9j9eF'><ins id='9j9eF'></ins><ul id='9j9eF'></ul><sub id='9j9eF'></sub></form><legend id='9j9eF'></legend><bdo id='9j9eF'><pre id='9j9eF'><center id='9j9eF'></center></pre></bdo></b><th id='9j9eF'></th></span></q></dt></tr></i><div id='9j9eF'><tfoot id='9j9eF'></tfoot><dl id='9j9eF'><fieldset id='9j9eF'></fieldset></dl></div>

        元组周围什么时候需要括号?

        时间:2023-09-01
        <tfoot id='8b0nm'></tfoot><legend id='8b0nm'><style id='8b0nm'><dir id='8b0nm'><q id='8b0nm'></q></dir></style></legend>
      1. <i id='8b0nm'><tr id='8b0nm'><dt id='8b0nm'><q id='8b0nm'><span id='8b0nm'><b id='8b0nm'><form id='8b0nm'><ins id='8b0nm'></ins><ul id='8b0nm'></ul><sub id='8b0nm'></sub></form><legend id='8b0nm'></legend><bdo id='8b0nm'><pre id='8b0nm'><center id='8b0nm'></center></pre></bdo></b><th id='8b0nm'></th></span></q></dt></tr></i><div id='8b0nm'><tfoot id='8b0nm'></tfoot><dl id='8b0nm'><fieldset id='8b0nm'></fieldset></dl></div>

        <small id='8b0nm'></small><noframes id='8b0nm'>

          <tbody id='8b0nm'></tbody>
            • <bdo id='8b0nm'></bdo><ul id='8b0nm'></ul>

                  本文介绍了元组周围什么时候需要括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在需要或不需要用括号括起来的元组时,是否有精确定义的引用?

                  Is there a reference somewhere defining precisely when enclosing tuples with parentheses is or is not required?

                  这是一个最近让我吃惊的例子:

                  Here is an example that surprised me recently:

                  >>> d = {}
                  >>> d[0,] = 'potato'
                  >>> if 0, in d:
                    File "<stdin>", line 1
                      if 0, in d:
                          ^
                  SyntaxError: invalid syntax
                  

                  推荐答案

                  使用逗号标记创建元组的表达式组合称为 expression_list.运算符优先级的规则不包括表达式列表;这是因为表达式列表本身不是表达式;括在括号中时它们成为表达式.

                  The combining of expressions to create a tuple using the comma token is termed an expression_list. The rules of operator precedence do not cover expression lists; this is because expression lists are not themselves expressions; they become expressions when enclosed in parentheses.

                  因此,在 Python 中语言语法明确允许的任何地方都允许使用未封闭的 expression_list,但 不允许其中 expression 为这是必需的.

                  So, an unenclosed expression_list is allowed anywhere in Python that it is specifically allowed by the language grammar, but not where an expression as such is required.

                  例如if语句的语法如下:

                  if_stmt ::=  "if" expression ":" suite
                               ( "elif" expression ":" suite )*
                               ["else" ":" suite]
                  

                  因为引用了产生式 expression,所以不允许未封闭的 expression_list 作为 if 语句的主题.但是,for 语句 接受 expression_list:

                  Because the production expression is referenced, unenclosed expression_lists are not allowed as the subject of the if statement. However, the for statement accepts an expression_list:

                  for_stmt ::=  "for" target_list "in" expression_list ":" suite
                                ["else" ":" suite]
                  

                  所以以下是允许的:

                  for x in 1, 2, 3:
                      print(x)
                  

                  这篇关于元组周围什么时候需要括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用多个 __init__ 参数子类化元组 下一篇:解压元组的pythonic方法是什么?

                  相关文章

                  <legend id='8PJyt'><style id='8PJyt'><dir id='8PJyt'><q id='8PJyt'></q></dir></style></legend>

                  <small id='8PJyt'></small><noframes id='8PJyt'>

                      <i id='8PJyt'><tr id='8PJyt'><dt id='8PJyt'><q id='8PJyt'><span id='8PJyt'><b id='8PJyt'><form id='8PJyt'><ins id='8PJyt'></ins><ul id='8PJyt'></ul><sub id='8PJyt'></sub></form><legend id='8PJyt'></legend><bdo id='8PJyt'><pre id='8PJyt'><center id='8PJyt'></center></pre></bdo></b><th id='8PJyt'></th></span></q></dt></tr></i><div id='8PJyt'><tfoot id='8PJyt'></tfoot><dl id='8PJyt'><fieldset id='8PJyt'></fieldset></dl></div>
                      • <bdo id='8PJyt'></bdo><ul id='8PJyt'></ul>
                      <tfoot id='8PJyt'></tfoot>