逗号语法:语句中出现逗号悬停的基本原理是SyntaxError [英] Comma Syntax: rationale behind a hanging comma in a statement being a SyntaxError

查看:56
本文介绍了逗号语法:语句中出现逗号悬停的基本原理是SyntaxError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,变量或文字后跟一个挂逗号是一个 tuple :

In Python, a variable or literal followed by a hanging comma is a one-tuple:

1, # (1,)

...以及一系列用逗号分隔的变量/文字(无论它们后面是否有一个逗号)也是一个 tuple :

...and a series of comma-separated variables/literals (whether or not they are followed by a hanging comma) is also a tuple:

1,2, # (1,2)
1,2 # (1,2)

但是,在可调用/函数内部,此语法的处理方式有所不同,因为逗号用于分隔参数:

However, inside a callable/function, this syntax is treated differently, because the comma is used for separation of arguments:

bool(5>6,) # False - 5>6 == False
bool((5>6,)) # True - (5>6,) is a non-empty tuple (always True - contents ignored)

第一行似乎只是忽略了逗号.第二行创建一个 tuple (如预期).对于用户定义的函数也是如此(不知道为什么不会这样):

The first line seems to simply ignore the hanging comma. The second line creates a one-tuple (as expected). This holds true for user-defined functions as well (don't know why it wouldn't):

def f(arg):
    pass
f(1,) # No Error 

还要考虑 assert 的以下行为(这是一条语句,而不是函数):

Consider also the following behavior of assert (which is a statement, not a function):

assert 5>6 # AssertionError, as expected 
assert(5>6) # AssertionError, as expected 
assert 5>6, # SyntaxError: invalid syntax
assert(5>6,) # SyntaxWarning: assertion always true, perhaps remove parentheses?
assert 5>6, 'NOPE!' # AssertionError: NOPE!, as expected 

因此,我对悬挂式逗号的治疗方法的解释如下:

Therefore my interpretation of the treatment of hanging commas is as follows:

  • 如果逗号在函数参数的上下文中,则会被忽略
  • 如果逗号位于语句上下文中,则表示语法错误
  • 在其他地方,它被解释为 tuple 对象
  • 的一部分
  • If the comma is in the context of function arguments, it is ignored
  • If the comma is in the context of a statement, it is a syntax error
  • Elsewhere, it is interpreted as part of a tuple object

我的问题:我对上述行为的解释正确吗?Python解释器是否只是忽略在参数列表中找到的挂起逗号?此行为会因Python实现而有所不同吗?最后:为什么在语句末尾(语法错误)和参数列表末尾(无语法错误)的挂逗号处理方式不一致?

My question: is my interpretation of the above behavior correct? Does the Python interpreter simply ignore hanging commas found in argument lists? Does this behavior vary by Python implementation? Finally: WHY is there an inconsistency in the treatment of a hanging comma at the end of a statement (syntax error) and at the end of an argument list (no syntax error)?

在阅读了答案并经过了更多思考之后,我的解释应作如下修改:

After reading the answers and thinking it through a bit more, my interpretation should be amended as follows:

  • 除以下两种情况外,挂逗号总是被忽略
  • 如果在语句的上下文中使用逗号,则表示语法错误
  • 如果挂逗号表示一个元组- tuple

但是,这仍然留下了一个问题,为什么当为函数提供参数时,忽略逗号作为提供给语句的参数.

However, this still leaves the question of WHY the hanging comma is not ignored for supplying arguments to statements, when they are ignored for supplying arguments to functions.

推荐答案

元组由逗号定义,除非,上下文为逗号定义了不同的含义.在这种情况下,您需要使用括号来消除什么是元组逗号和其他逗号.

A tuple is defined by the comma, unless the context has defined a different meaning for the comma. In that case you need to use parentheses to disambiguate what is a tuple comma and a different comma.

assert 不是函数,它是语句,因此parethese不是语法的一部分.逗号,因此您需要使用圆括号将元组与断言表达式和失败消息之间的逗号区分开来,但是仍然需要使用逗号来定义元组.

assert is not a function, it is a statement, so paretheses are not part of the syntax. Commas are, so you need the parentheses to disambiguate the tuple from the comma between the assertion expression and the failure message, but you still need the comma there to define the tuple.

在定义元组并在调用表达式中使用a时,始终都会忽略多余的尾部逗号.但是,您需要知道何时创建元组以及何时将逗号用于其他表达式.要创建一个元组,您至少需要一个逗号,而在调用中则不需要该逗号,因为调用语法不是由逗号运算符定义的.

Both when defining a tuple and using a in a call expression, a surplus trailing comma is ignored, consistently. But you need to be aware of when you are creating a tuple and when you are using the comma for a different expression. To create a tuple, you need at least one comma, while in a call you don't need to have that comma because the call syntax is not defined by the comma operator.

摘录自 表达列表 :

包含至少一个逗号的表达式列表会产生一个元组.元组的长度是列表中表达式的数量.表达式从左到右求值.

An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.

仅需使用尾部逗号才能创建单个元组(也称为 singleton );在所有其他情况下都是可选的.没有尾部逗号的单个表达式不会创建元组,而是会产生该表达式的值.(要创建一个空的元组,请使用一对空的括号:().)

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)

并来自 整形表格 :

带括号的形式是用括号括起来的可选表达式列表:

A parenthesized form is an optional expression list enclosed in parentheses:

parenth_form ::=  "(" [expression_list] ")"

带括号的表达式列表将产生该表达式列表产生的任何结果:如果该列表包含至少一个逗号,它将产生一个元组;否则,它会产生一个元组.否则,将生成组成表达式列表的单个表达式.

A parenthesized expression list yields whatever that expression list yields: if the list contains at least one comma, it yields a tuple; otherwise, it yields the single expression that makes up the expression list.

一对空括号会产生一个空的元组对象.由于元组是不可变的,因此适用文字规则(即两次出现的空元组可能会或可能不会产生相同的对象).

An empty pair of parentheses yields an empty tuple object. Since tuples are immutable, the rules for literals apply (i.e., two occurrences of the empty tuple may or may not yield the same object).

请注意,元组不是由括号形成的,而是由逗号运算符形成的.空元组是一个例外,它需要括号 -允许在表达式中使用无括号的"nothing"会引起歧义并允许常见的错别字通过.

Note that tuples are not formed by the parentheses, but rather by use of the comma operator. The exception is the empty tuple, for which parentheses are required — allowing unparenthesized "nothing" in expressions would cause ambiguities and allow common typos to pass uncaught.

强调我的.

最后,来自 Calls 文档:

Finally, from the Calls documentation:

位置和关键字参数后可能会出现逗号,但不会影响语义.

A trailing comma may be present after the positional and keyword arguments but does not affect the semantics.

这篇关于逗号语法:语句中出现逗号悬停的基本原理是SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆