Why tuples use parentheses ()'s instead of something else like <>'s?
Alex Martelli
aleaxit at yahoo.com
Wed Dec 29 02:49:44 EST 2004
seberino at spawar.navy.mil <seberino at spawar.navy.mil> wrote:
> Tuples are defined with regards to parentheses ()'s as everyone knows.
Well, then, "everyone knows" wrong:
x = 1, 2, 3
x is a tuple. The _commas_ make it one -- parentheses don't matter.
An _empty_ tuple uses parentheses, (), as there's nowhere to put commas;
and you need parentheses AROUND the tuple-with-commas when the commas by
themselves would be interpreted otherwise (function definition and call,
except clause). But generally, the commas are what mattes.
> This causes confusion for 1 item tuples since (5) can be interpreted
> as a tuple OR as the number 5 in a mathematical expression
Nah: no commas, no tuple. To set x to a one-item tuple:
x = 5,
feel free to put useless parentheses around the RHS, they don't hurt.
But the comma MUST be there.
> Wouldn't it have been better to define tuples with <>'s or {}'s or
> something else to avoid this confusion??
Instead of commas? I think it would look weird.
> Perhaps ()'s are a good idea for some other reason I don't know?
They're somewhat overloaded, and so are commas. There just isn't enough
neat-looking punctuation in the ASCII character set.
Alex
More information about the Python-list
mailing list