在命令、列表或集合末尾的额外逗号有什么特殊意义吗?

时间:2021-11-29 19:11:25

I noticed by chance that adding an extra separator comma at the end of a list, dict or set is syntactically correct and does not seem to add anything to the data structure:

我偶然注意到,在列表、dict或set的末尾添加一个额外的分隔符,在语法上是正确的,并且似乎没有向数据结构添加任何内容:

In [1]: d1 = {'a': 1, 'b': 2}

In [2]: d2 = {'c': 10, 'd': 20,}

In [3]: d1
Out[3]: {'a': 1, 'b': 2}

In [4]: d2
Out[4]: {'c': 10, 'd': 20}

Does it have any special meaning or usage? The only one I found is to explicit a data structure during an initialization:

它有什么特殊的含义或用法吗?我发现的唯一一种方法是在初始化过程中显示数据结构:

In [14]: r = (1)

In [15]: r
Out[15]: 1

In [16]: r = (1,)

In [17]: r
Out[17]: (1,)

3 个解决方案

#1


17  

It has no special meaning in a list or dictionary, but can be useful when using source code change management tools, see below.

它在列表或字典中没有特殊的含义,但是在使用源代码变更管理工具时很有用,请参见下面。

Non-empty tuples are defined by using a comma between elements, the parentheses are optional and only required in contexts where the comma could have a different meaning.

非空元组是通过在元素之间使用逗号定义的,括号是可选的,只在逗号可以有不同含义的上下文中需要。

Because the comma defines the tuple, you need at least one comma if there is just the one element:

因为逗号定义了元组,如果只有一个元素,那么至少需要一个逗号:

>>> 1
1
>>> 1,
(1,)
>>> type((1,)) # need parens to distinguish comma from the argument separator
<type 'tuple'>

The empty tuple is defined by using empty parentheses:

空的元组是用空括号定义的:

>>> type(())
<type 'tuple'>

The trailing comma can be helpful in minimising how many lines changed when adding new lines; adding an additional line to a dictionary with a trailing comma would not change the last existing entry:

在添加新行时,尾逗号可以帮助最小化更改了多少行;在带有逗号的字典中添加附加行不会更改最后一个已存在的条目:

a_value = {
    key1: value1,
    key2: value2,
    # inserting here doesn't require adding a comma
    # to the preceding line.
}

#2


6  

It’s useful for consistency across multiple lines:

它对跨多行一致性很有用:

d1 = {
    'a': 1,
    'b': 2,
}

You can rearrange or add lines without changing commas. It doesn’t change anything about the information represented.

您可以重新排列或添加行,而不需要更改逗号。它对所表示的信息没有任何改变。

#3


2  

You can use dis module to verify that behaviour of both objects is exactly the same.

您可以使用dis模块来验证两个对象的行为完全相同。

In [10]: import dis

In [11]: def make_dict():
    return {"a": 1, "b": 2}
   ....: 

In [12]: def make_dict_2():
    return {"a": 1, "b": 2,}
   ....: 

In [13]: dis.dis(make_dict)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE        

In [14]: dis.dis(make_dict_2)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE 

#1


17  

It has no special meaning in a list or dictionary, but can be useful when using source code change management tools, see below.

它在列表或字典中没有特殊的含义,但是在使用源代码变更管理工具时很有用,请参见下面。

Non-empty tuples are defined by using a comma between elements, the parentheses are optional and only required in contexts where the comma could have a different meaning.

非空元组是通过在元素之间使用逗号定义的,括号是可选的,只在逗号可以有不同含义的上下文中需要。

Because the comma defines the tuple, you need at least one comma if there is just the one element:

因为逗号定义了元组,如果只有一个元素,那么至少需要一个逗号:

>>> 1
1
>>> 1,
(1,)
>>> type((1,)) # need parens to distinguish comma from the argument separator
<type 'tuple'>

The empty tuple is defined by using empty parentheses:

空的元组是用空括号定义的:

>>> type(())
<type 'tuple'>

The trailing comma can be helpful in minimising how many lines changed when adding new lines; adding an additional line to a dictionary with a trailing comma would not change the last existing entry:

在添加新行时,尾逗号可以帮助最小化更改了多少行;在带有逗号的字典中添加附加行不会更改最后一个已存在的条目:

a_value = {
    key1: value1,
    key2: value2,
    # inserting here doesn't require adding a comma
    # to the preceding line.
}

#2


6  

It’s useful for consistency across multiple lines:

它对跨多行一致性很有用:

d1 = {
    'a': 1,
    'b': 2,
}

You can rearrange or add lines without changing commas. It doesn’t change anything about the information represented.

您可以重新排列或添加行,而不需要更改逗号。它对所表示的信息没有任何改变。

#3


2  

You can use dis module to verify that behaviour of both objects is exactly the same.

您可以使用dis模块来验证两个对象的行为完全相同。

In [10]: import dis

In [11]: def make_dict():
    return {"a": 1, "b": 2}
   ....: 

In [12]: def make_dict_2():
    return {"a": 1, "b": 2,}
   ....: 

In [13]: dis.dis(make_dict)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE        

In [14]: dis.dis(make_dict_2)
  2           0 BUILD_MAP                2
              3 LOAD_CONST               1 (1)
              6 LOAD_CONST               2 ('a')
              9 STORE_MAP           
             10 LOAD_CONST               3 (2)
             13 LOAD_CONST               4 ('b')
             16 STORE_MAP           
             17 RETURN_VALUE