Velocity Reviews > Why tuples use parentheses ()'s instead of something else like <>'s?

# Why tuples use parentheses ()'s instead of something else like <>'s?

seberino@spawar.navy.mil
Guest
Posts: n/a

 12-29-2004
Tuples are defined with regards to parentheses ()'s as everyone knows.

This causes confusion for 1 item tuples since (5) can be interpreted
as a tuple OR as the number 5 in a mathematical expression
such as x = (5) * (4+6).

Wouldn't it have been better to define tuples with <>'s or {}'s or
something else to avoid this confusion??

Perhaps ()'s are a good idea for some other reason I don't know?

Please enlighten me as I really want to know.
Chris

P.S. I love Python!

Marius Bernklev
Guest
Posts: n/a

 12-29-2004
* http://www.velocityreviews.com/forums/(E-Mail Removed)

> Perhaps ()'s are a good idea for some other reason I don't know?

One-element tuples are written as (4,).

--
Marius Bernklev

<URL: http://www.ping.uio.no/~mariube/ >

Hans Nowak
Guest
Posts: n/a

 12-29-2004
(E-Mail Removed) wrote:

> Tuples are defined with regards to parentheses ()'s as everyone knows.
>
> This causes confusion for 1 item tuples since (5) can be interpreted
> as a tuple OR as the number 5 in a mathematical expression
> such as x = (5) * (4+6).

No, (5) is always the number 5. To make a one-element tuple, use (5,).

> Wouldn't it have been better to define tuples with <>'s or {}'s or
> something else to avoid this confusion??
>
> Perhaps ()'s are a good idea for some other reason I don't know?

Actually, for non-empty tuples, the parentheses aren't really necessary,
unless code is ambiguous.

>>> x = 1, 2, 3
>>> x

(1, 2, 3)
>>> y = 5,
>>> y

(5,)

but:

>>> print 8, 9 # not a tuple

8 9
>>> print (8, 9)

(8, 9)

HTH,

--
Hans Nowak
http://zephyrfalcon.org/

Leif K-Brooks
Guest
Posts: n/a

 12-29-2004
(E-Mail Removed) wrote:
> Wouldn't it have been better to define tuples with <>'s or {}'s or
> something else to avoid this confusion??

The way I see it, tuples are just a way of having a function return
multiple values at once. When you think of them that way, you don't even
need parenthesis:

def foo():
if we_found_stuff:
return 200, 'long and boring result'
else:
return 404, 'nothing found'

status_code, body = foo()

If foo() only needed to return one value, it would do so in the normal
way, and you wouldn't need to worry about 1-tuples.

Alex Martelli
Guest
Posts: n/a

 12-29-2004
(E-Mail Removed) <(E-Mail Removed)> 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

Brian Beck
Guest
Posts: n/a

 12-29-2004
(E-Mail Removed) wrote:
> Wouldn't it have been better to define tuples with <>'s or {}'s or
> something else to avoid this confusion??

Well, to comment on the part that nobody else did...
< and > are binary operators, a la 3 > 1, "one" < "two"
and {}'s are clearly already used for dictionaries.

--
Brian Beck

Steve Holden
Guest
Posts: n/a

 12-29-2004
Marius Bernklev wrote:

> * (E-Mail Removed)
>
>
>>Perhaps ()'s are a good idea for some other reason I don't know?

>
>
> One-element tuples are written as (4,).
>

And, even there, the parenthesis is only required when it would
otherwise be embiguous:

>>> x = 4,
>>> x

(4,)
>>> print 4,

4
>>>

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119

John Roth
Guest
Posts: n/a

 12-29-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Tuples are defined with regards to parentheses ()'s as everyone knows.

To expand on what Alex Martelli said:

Tuples don't use parentheses except for the special case of the
empty tuple. Those are expression parentheses. The two most
obvious cases of this are in the return statement and sequence
unpacking in assignment statements.

Grouping syntax is used for both unary operators and operands.
Parentheses are used for expressions (operands) and
function/method parameter lists (operators). Brackets ([])
are used for lists (operands) and subscripts/slices (operators).
Braces ({}) are used for dictionarys (operands). They aren't
currently used for unary operators.

John Roth

> Please enlighten me as I really want to know.
> Chris
>
> P.S. I love Python!
>

Grant Edwards
Guest
Posts: n/a

 12-29-2004
On 2004-12-29, (E-Mail Removed) <(E-Mail Removed)> 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".

--
Grant Edwards grante Yow! I'm working under
at the direct orders of WAYNE
visi.com NEWTON to deport consenting

Roy Smith
Guest
Posts: n/a

 12-29-2004
In article <41d2ce43\$0\$35731\$(E-Mail Removed)>,
Grant Edwards <(E-Mail Removed)> wrote:

> On 2004-12-29, (E-Mail Removed) <(E-Mail Removed)> 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).