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

Hans Nowak hans at zephyrfalcon.org
Wed Dec 29 01:25:03 EST 2004


seberino at spawar.navy.mil 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/




More information about the Python-list mailing list