[Python-ideas] One-element tuple

Greg Falcon veloso at verylowsodium.com
Fri Mar 14 18:56:00 CET 2008


On 3/13/08, Leszek Dubiel <leszek at dubiel.pl> wrote:
>  I would suggest to deprecate one-element tuple construction with colon at
> the end, because this looks ugly, is not self-evident for other people
> reading code, looks like some type of trickery.

Notice that nearly all reasonable programming languages allow for an
extra trailing comma in a comma-delimited list, for consistency and to
make programmatic code generation easier.  So for example, [1,2,3,] is
intentionally correct.

One you accept (1,2,3,) as a reasonable tuple, you'll see that
disallowing or deprecating (1,) is wrong, too.

> >>> tuple(['hello'])
>  ('hello',)

Tuples are a fundamental data type, and it would be irresponsible to
steer beginners away from their simple literal syntax.

It's mildly unfortunate that (1,2,) (1,2) (1,) and () represent tuples
but (1) doesn't.  But this rule is simple, well motivated, and
described quite straightforwardly at the very page you link to.
Better to leave it as is, so beginners can learn the rule, accept it,
and move on.

Greg F



More information about the Python-ideas mailing list