"Data blocks" syntax specification draft

Chris Angelico rosuav at gmail.com
Tue May 22 11:57:13 EDT 2018


On Wed, May 23, 2018 at 1:43 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Tue, May 22, 2018 at 9:34 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Wed, May 23, 2018 at 1:22 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>> On Tue, May 22, 2018 at 8:25 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>>> On Tue, May 22, 2018 at 8:25 PM, bartc <bc at freeuk.com> 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].
>>
>> In an arbitrary expression, a comma between two expressions creates a
>> tuple. In other contexts, the comma has other meanings, which take
>> precedence:
>>
>> * Separating a function's arguments (both at definition and call)
>> * Enumerating import targets and global/nonlocal names
>> * Separating an assertion from its message
>> * Listing multiple context managers
>> * And probably some that I've forgotten.
>>
>> In those contexts, you can override the normal interpretation and
>> force the tuple by using parentheses, preventing it from being parsed
>> as something else, and making it instead a single expression:
>>
>> print((1, 2)) # prints a tuple
>> print(1, 2) # prints two items
>>
>> The comma is what makes the tuple, though, not the parentheses. The
>> parentheses merely prevent this from being something else.
>
> In other words, the rule is not really as simple as "commas make
> tuples". I stand by what I wrote.

Neither of us is wrong here. "Commas make tuples" is a useful
oversimplification in the same way that "asterisk means
multiplication" is. The asterisk has other meanings in specific
contexts (eg unpacking), but outside of those contexts, it means
multiplication.

ChrisA



More information about the Python-list mailing list