问题:在元组定义中使用逗号结尾的语法规则是什么?
在单个元素元组的情况下,需要尾随逗号。
a = ('foo',)
那么具有多个元素的元组呢?似乎尾随逗号是否存在,它们都有效。这样对吗?我认为使用逗号结尾更易于编辑。那是不好的编码风格吗?
a = ('foo1', 'foo2')
b = ('foo1', 'foo2',)
In the case of a single element tuple, the trailing comma is required.
a = ('foo',)
What about a tuple with multiple elements? It seems that whether the trailing comma exists or not, they are both valid. Is this correct? Having a trailing comma is easier for editing in my opinion. Is that a bad coding style?
a = ('foo1', 'foo2')
b = ('foo1', 'foo2',)
回答 0
在所有情况下,除了空的元组,逗号都是重要的事情。仅在出于其他语法原因而需要时才需要括号:将元组与一组函数参数,运算符优先级区分开或允许换行。
元组,列表或函数参数的尾部逗号是一种不错的样式,尤其是当您有一个较长的初始化并分为多行时。如果始终包含尾随逗号,则不会在末尾添加任何行,而期望添加另一个元素,而只是创建一个有效的表达式:
a = [
"a",
"b"
"c"
]
假设它最初是一个2元素列表,后来又进行了扩展,那么它可能以一种可能并不立即显而易见的方式出错了。始终包含结尾逗号,这样可以避免出现陷阱。
In all cases except the empty tuple the comma is the important thing. Parentheses are only required when required for other syntactic reasons: to distinguish a tuple from a set of function arguments, operator precedence, or to allow line breaks.
The trailing comma for tuples, lists, or function arguments is good style especially when you have a long initialisation that is split over multiple lines. If you always include a trailing comma then you won’t add another line to the end expecting to add another element and instead just creating a valid expression:
a = [
"a",
"b"
"c"
]
Assuming that started as a 2 element list that was later extended it has gone wrong in a perhaps not immediately obvious way. Always include the trailing comma and you avoid that trap.
回答 1
只有单项元组才需要消除定义元组或由括号包围的表达式的歧义。
(1) # the number 1 (the parentheses are wrapping the expression `1`)
(1,) # a 1-tuple holding a number 1
对于一个以上的项目,不再需要了,因为显然它是一个元组。但是,允许使用逗号逗号来简化使用多行定义它们的过程。您可以在不破坏语法的情况下添加到末尾或重新排列项目,因为您偶然遗漏了逗号。
例如,
someBigTuple = (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
#...
10000000000,
)
请注意,这也适用于其他集合(例如列表和字典),而不仅仅是元组。
It is only required for single-item tuples to disambiguate defining a tuple or an expression surrounded by parentheses.
(1) # the number 1 (the parentheses are wrapping the expression `1`)
(1,) # a 1-tuple holding a number 1
For more than one item, it is no longer necessary since it is perfectly clear it is a tuple. However, the trailing comma is allowed to make defining them using multiple lines easier. You could add to the end or rearrange items without breaking the syntax because you left out a comma on accident.
e.g.,
someBigTuple = (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
#...
10000000000,
)
Note that this applies to other collections (e.g., lists and dictionaries) too and not just tuples.
回答 2
尾随逗号的另一个优点是,它使差异看起来更好。如果您从
a = [
1,
2,
3
]
并将其更改为
a = [
1,
2,
3,
4
]
差异看起来像
a = [
1,
2,
- 3
+ 3,
+ 4
]
而如果您以逗号开头,例如
a = [
1,
2,
3,
]
然后差异将是
a = [
1,
2,
3,
+ 4,
]
Another advantage of trailing commas is that it makes diffs look nicer. If you started with
a = [
1,
2,
3
]
and changed it to
a = [
1,
2,
3,
4
]
The diff would look like
a = [
1,
2,
- 3
+ 3,
+ 4
]
Whereas if you had started with a trailing comma, like
a = [
1,
2,
3,
]
Then the diff would just be
a = [
1,
2,
3,
+ 4,
]
回答 3
It’s optional: see the Python wiki.
Summary: single-element tuples need a trailing comma, but it’s optional for multiple-element tuples.
回答 4
另外,请考虑所需的情况:
>>> (('x','y'))*4 # same as ('x','y')*4
('x', 'y', 'x', 'y', 'x', 'y', 'x', 'y')
#Expected = (('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))
因此,在这种情况下,外部括号仅是对括号进行分组。要使它们成为元组,您需要添加尾随逗号。即
>>> (('x','y'),)*4
(('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))
Also, consider the situation where you want:
>>> (('x','y'))*4 # same as ('x','y')*4
('x', 'y', 'x', 'y', 'x', 'y', 'x', 'y')
#Expected = (('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))
So in this case the outer parentheses are nothing more than grouping parentheses. To make them tuple you need to add a trailing comma. i.e.
>>> (('x','y'),)*4
(('x', 'y'), ('x', 'y'), ('x', 'y'), ('x', 'y'))
回答 5
仅尾元素元组才需要尾随逗号。对于较大的元组,用逗号结尾是一个样式问题,并非必需。它的最大优点是对带有经常修改的多行大元组(例如配置元组)的文件进行完全比较。
Trailing comma is required for one-element tuples only. Having a trailing comma for larger tuples is a matter of style and is not required. Its greatest advantage is clean diff on files with multi-line large tuples that are often modified (e.g. configuration tuples).
回答 6
这是一个简单的答案。
a =(“ s”)是一个字符串
和
a =(“ s”,)是具有一个元素的元组。
对于一个元素元组,Python需要附加逗号,以区分字符串和元组。
例如,在python控制台上尝试:
a =(“ s”)
a = a +(1,2,3)
追溯(最近一次通话):
文件stdin,第1行,在模块中
TypeError:无法连接“ str”和“ tuple”对象
That’s a simple answer.
a = (“s”) is a string
and
a = (“s”,) is a tuple with one element.
Python needs an additional comma in case of one element tuple to, differentiate between string and tuple.
For example try this on python console:
a = (“s”)
a = a + (1,2,3)
Traceback (most recent call last):
File stdin, line 1, in module
TypeError: cannot concatenate ‘str’ and ‘tuple’ objects
回答 7
存在的另一个原因是它使代码生成和__repr__
功能更易于编写。例如,如果您有一些构建为的对象obj(arg1, arg2, ..., argn)
,则可以这样写obj.__repr__
:
def __repr__(self):
l = ['obj(']
for arg in obj.args: # Suppose obj.args == (arg1, arg2, ..., argn)
l.append(repr(arg))
l.append(', ')
l.append(')')
return ''.join(l)
如果不允许使用逗号结尾,则必须对最后一个参数进行特殊处理。实际上,您可以使用列表理解将上述内容写在一行中(我将其写得更长一些,以便于阅读)。如果上学期不得不特殊情况,要做到这一点并非易事。
Another reason that this exists is that it makes code generation and __repr__
functions easier to write. For example, if you have some object that is built like obj(arg1, arg2, ..., argn)
, then you can just write obj.__repr__
as
def __repr__(self):
l = ['obj(']
for arg in obj.args: # Suppose obj.args == (arg1, arg2, ..., argn)
l.append(repr(arg))
l.append(', ')
l.append(')')
return ''.join(l)
If a trailing comma wasn’t allowed, you would have to special case the last argument. In fact, you could write the above in one line using a list comprehension (I’ve written it out longer to make it easier to read). It wouldn’t be so easy to do that if you had to special case the last term.
回答 8
PEP 8-Python代码样式指南-何时使用尾随逗号
尾部的逗号通常是可选的,但当组成一个元素的元组时它们是必需的(并且在Python 2中,它们具有print语句的语义)。为了清楚起见,建议将后者用(技术上多余的)括号括起来。
是:
FILES = ('setup.cfg',)
好的,但是令人困惑:
FILES = 'setup.cfg',
如果结尾的逗号多余,则在使用版本控制系统时,当值,参数或导入项的列表预计会随着时间扩展时,它们通常会很有用。模式是将每个值(等)单独放在一行上,始终添加尾随逗号,并在下一行上添加右括号/括号/括号。但是,在与结束定界符相同的行上使用尾随逗号是没有意义的(在上述单例元组的情况下除外)。
是:
FILES = [
'setup.cfg',
'tox.ini',
]
initialize(FILES,
error=True,
)
没有:
FILES = ['setup.cfg', 'tox.ini',]
initialize(FILES, error=True,)
PEP 8 — Style Guide for Python Code – When to Use Trailing Commas
Trailing commas are usually optional, except they are mandatory when making a tuple of one element (and in Python 2 they have semantics for the print statement). For clarity, it is recommended to surround the latter in (technically redundant) parentheses.
Yes:
FILES = ('setup.cfg',)
OK, but confusing:
FILES = 'setup.cfg',
When trailing commas are redundant, they are often helpful when a version control system is used, when a list of values, arguments or imported items is expected to be extended over time. The pattern is to put each value (etc.) on a line by itself, always adding a trailing comma, and add the close parenthesis/bracket/brace on the next line. However it does not make sense to have a trailing comma on the same line as the closing delimiter (except in the above case of singleton tuples).
Yes:
FILES = [
'setup.cfg',
'tox.ini',
]
initialize(FILES,
error=True,
)
No:
FILES = ['setup.cfg', 'tox.ini',]
initialize(FILES, error=True,)
回答 9
编码风格是您的喜好,如果您认为编码标准很重要,那么可以使用PEP-8指导您。
您如何看待以下表达的结果?
x = (3)
x = (3+2)
x = 2*(3+2)
是的,x只是一个数字。
Coding style is your taste, If you think coding standard matters there is a PEP-8 That can guide you.
What do you think of the result of following expression?
x = (3)
x = (3+2)
x = 2*(3+2)
Yep, x is just an number.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。