Bri*_*eud 6 python syntax parentheses
谁能告诉我为什么括号加倍?
self.__items.append((module, item))
Run Code Online (Sandbox Code Playgroud)
sen*_*rle 12
内括号创建一个元组.
>>> type(('a', 'b'))
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)
从技术上讲,可以创建没有括号的元组:
>>> 'a', 'b'
('a', 'b')
Run Code Online (Sandbox Code Playgroud)
但有时他们需要括号:
>>> 'a', 'b' + 'c', 'd'
('a', 'bc', 'd')
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')
Run Code Online (Sandbox Code Playgroud)
在您的情况下,他们需要括号来区分元组和逗号分隔的参数到函数.例如:
>>> def takes_one_arg(x):
... return x
...
>>> takes_one_arg('a', 'b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: takes_one_arg() takes exactly 1 argument (2 given)
>>> takes_one_arg(('a', 'b'))
('a', 'b')
Run Code Online (Sandbox Code Playgroud)
它将元组(module, item)作为单个参数传递给函数.没有额外的parens,它将通过module并item作为单独的论点.