Can anyone tell me why the parenthesis are doubled here?
self.__items.append((module, item))
The inner parenthesis create a tuple.
>>> type(('a', 'b'))
<type 'tuple'>
Technically, tuples can be created without parenthesis:
>>> 'a', 'b'
('a', 'b')
But sometimes they need parenthesis:
>>> 'a', 'b' + 'c', 'd'
('a', 'bc', 'd')
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')
In your case, they need parenthesis to distinguish the tuple from the comma-separated arguments to a function. For example:
>>> 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')
It's passing the tuple (module, item)
to the function as a single argument. Without the extra parens, it would pass module
and item
as separate arguments.
That's exactly the same as saying:
parameter = (module, item)
self.__items.append(parameter)
I.e. the inner parens are first creating a tuple before the tuple is used as the single argument to append()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With