Why tuples use parentheses ()'s instead of something else like <>'s?
roy at panix.com
Wed Dec 29 17:08:18 CET 2004
In article <41d2ce43$0$35731$a1866201 at visi.com>,
Grant Edwards <grante at visi.com> wrote:
> On 2004-12-29, seberino at spawar.navy.mil <seberino at spawar.navy.mil> wrote:
> > Tuples are defined with regards to parentheses ()'s as everyone knows.
> Except they're not.
> >>> x = 1,2,3,4
> >>> type(x)
> <type 'tuple'>
> Tuples are defined by the infix comma "operator".
Well, the syntax is a little more complicated than that. Commas don't
form tuples in a lot of places:
f (1, 2, 3) # function call gets three scalar arguments
[1, 2, 3] # list of three integers, not list of tuple
[x, 1 for x in blah] # syntax error, needs to be [(x, 1) for ...]
I'm sure there are others. The meaning of "," depends on the context in
which it appears. In most cases, the parens around tuples are optional
except when necessary to disambiguate, but there's one degenerate
special case, the empty tuple (zerople?), where the parens are always
required. It's just one of those little warts you have to live with.
If Python had originally been invented in a unicode world, I suppose we
wouldn't have this problem. We'd just be using guillemots for tuples
(and have keyboards which made it easy to type them).
More information about the Python-list