On Tue, May 22, 2018 at 8:25 AM, Chris Angelico <[email protected]> wrote:
> On Tue, May 22, 2018 at 8:25 PM, bartc <[email protected]> wrote:
>> Note that Python tuples don't always need a start symbol:
>>
>> a = 10,20,30
>>
>> assigns a tuple to a.
>
> The tuple has nothing to do with the parentheses, except for the
> special case of the empty tuple. It's the comma.
Although, if the rule were really as simple as "commas make tuples",
then this would be a list containing a tuple: [1, 2, 3].
Curiously, parentheses are also sometimes required for iterable
unpacking. For example:
py> 1, 2, *range(3,5)
(1, 2, 3, 4)
py> d = {}
py> d[1, 2] = 42
py> d[1, 2, *range(3,5)] = 43
File "<stdin>", line 1
d[1, 2, *range(3,5)] = 43
^
SyntaxError: invalid syntax
py> def foo():
... return 1, 2
...
py> foo()
(1, 2)
py> def foo():
... return 1, 2, *range(3, 5)
File "<stdin>", line 2
return 1, 2, *range(3, 5)
^
SyntaxError: invalid syntax
py> def foo():
... yield 1, 2
...
py> list(foo())
[(1, 2)]
py> def foo():
... yield 1, 2, *range(3, 5)
File "<stdin>", line 2
yield 1, 2, *range(3, 5)
^
SyntaxError: invalid syntax
py> for x in 1, 2: print(x)
...
1
2
py> for x in 1, 2, *range(3, 5): print(x)
File "<stdin>", line 1
for x in 1, 2, *range(3, 5): print(x)
^
SyntaxError: invalid syntax
--
https://mail.python.org/mailman/listinfo/python-list