Parens do create a tuple

Steven D'Aprano steve at pearwood.info
Sun Apr 10 22:57:37 EDT 2016


On Mon, 11 Apr 2016 11:51 am, Chris Angelico wrote:

> On Mon, Apr 11, 2016 at 11:41 AM, Ben Finney <ben+python at benfinney.id.au>
> wrote:
>>> I'd rather be correct on the one-element case and wrong on the empty
>>> than the other way around.
>>
>> To say “commas create tuples” is to say an unobjectionably true
>> statement about Python syntax. It remains true as one continues to learn
>> Python.
>>
>> To say “parens do not create tuples” is to lay a trap which needs to be
>> de-fused at some point. Better IMO never to lay that trap.
> 
> Fair. "Commas create tuples" is correct but incomplete; 

It's incorrect as well as incomplete. Not every comma is part of a tuple.

It's not even correct if you limit yourself to expressions, rather than all
Python statements. There are two commas in both the following expressions:

    (lambda a, b: a+2*b)(1, 2)

    "abc,def".split(",")


and yet no tuples are involved.

Yes, I'm being pedantic. I'm being more pedantic than Ben, and that's a
scary thing :-)


> "parens do not create tuples" is incorrect in a narrow way. 

It's only incorrect if you neglect to follow up by saying "with the
exception of the empty tuple". Which makes it incomplete rather than
incorrect. It is *certainly true* that in the expression:

    (1, 2, 3, 4)

the parens do not create the tuple, they are only used for grouping. So it
is misleading to say that '"parens do not create tuples" is incorrect'.


> Fortunately, technical 
> correctness lines up with the more useful case of helping people
> understand the one-element case.

You know, I've never come across anyone who has failed to understand the
one-element case from an explanation similar to this:

"Parentheses or round brackets don't create tuples, they are used for
grouping. It is the commas, not the brackets, that creates the tuple. The
only exception is the empty tuple, which is written as ()."

*especially* if you give an example of a one-element tuple to reinforce the
lesson.

Short, snappy, memorable statements like "parens don't create tuples" don't
have to be pedantically correct in all the technical details to be useful.
It is allowed to follow them with a more detailed explanation, including
any exceptions to the rule.



-- 
Steven




More information about the Python-list mailing list