逗号语法:在语句中挂起逗号后面的原因是一个SyntaxError(Comma Syntax: rationale behind a hanging comma in a statement being

编程入门 行业动态 更新时间:2024-10-27 07:24:56
逗号语法:在语句中挂起逗号后面的原因是一个SyntaxError(Comma Syntax: rationale behind a hanging comma in a statement being a SyntaxError)

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

1, # (1,)

...和一系列逗号分隔的变量/文字(不管它们后面是否挂上一个逗号)也是一个tuple :

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

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

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

第一行似乎完全忽略了悬挂的逗号。 第二行创建一个tuple (如预期的那样)。 这也适用于用户定义的函数(不知道为什么它不会):

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

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

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

因此,我对悬挂逗号处理的解释如下:

如果逗号在函数参数的上下文中,它将被忽略 如果逗号在语句的上下文中,则是语法错误 在其他地方,它被解释为tuple对象的一部分

我的问题是:我对上述行为的解释是否正确? Python解释器是否简单地忽略在参数列表中找到的悬挂逗号? 这种行为是否因Python实现而异? 最后:为什么在语句末尾(语法错误)和参数列表末尾(无语法错误)处理悬挂逗号时存在不一致?

编辑:在阅读答案后再思考一下,我的解释应该修改如下:

除了以下两种情况之外,逗号总是忽略 如果挂起的逗号在语句的上下文中,则是语法错误 如果悬挂的逗号表示一个tuple

然而,这仍然留下了一个问题,为什么当为语句提供参数时忽略悬挂逗号,因为为功能提供参数而忽略它们。

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

1, # (1,)

...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)

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

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:

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

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)?

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

The hanging comma is ALWAYS IGNORED, except in the following two contexts If the hanging comma is in the context of a statement, it is a syntax error If the hanging comma signals a one-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.

最满意答案

一个元组由逗号定义, 除非上下文为逗号定义了不同的含义。 在这种情况下,您需要使用括号来区分什么是元组逗号和不同的逗号。

assert不是一个函数,它是一个声明,所以遗传不是语法的一部分。 逗号 ,所以你需要括号来从断言表达式和失败消息之间的逗号中消除元组歧义,但是你仍然需要用逗号来定义元组。

无论是定义一个元组还是在调用表达式中使用一个元素,都会一直忽略多余的尾随逗号。 但是您需要了解何时创建元组以及何时使用逗号来表示不同的表达式。 要创建元组,至少需要一个逗号,而在调用中,不需要使用逗号,因为调用语法不是由逗号操作符定义的。

表达式列表的文档

包含至少一个逗号的表达式列表产生一个元组。 元组的长度是列表中的表达式的数量。 表达式从左到右进行评估。

尾随逗号仅需要创建单个元组(又名单身人士 ); 在所有其他情况下它是可选的。 没有尾随逗号的单个表达式不会创建元组,而是会生成该表达式的值。 (要创建一个空元组,请使用一对空括号:( () 。)

并从Parethesized形式

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

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

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

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

请注意,元组不是由括号组成,而是由逗号运算符组成。 例外是需要括号的空元组 - 允许表达式中没有“没有”会导致含糊不清,并允许常见的拼写错误未被捕获。

强调我的。

最后,从Calls文档 :

尾部逗号可能出现在位置和关键字参数之后,但不影响语义。

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 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.

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.

From the documentation on Expression lists:

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.

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: ().)

and from Parethesized forms:

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).

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.

Emphasis mine.

Finally, from the Calls documentation:

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

更多推荐

本文发布于:2023-08-02 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   是一个   语句   语法   挂起

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!