Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Why tuples use parentheses ()'s instead of something else like <>'s? (http://www.velocityreviews.com/forums/t339763-why-tuples-use-parentheses-s-instead-of-something-else-like-s.html)

 seberino@spawar.navy.mil 12-29-2004 05:57 AM

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

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 12-29-2004 06:14 AM

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

* seberino@spawar.navy.mil

> 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 12-29-2004 06:25 AM

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

seberino@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/

 Leif K-Brooks 12-29-2004 06:37 AM

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

seberino@spawar.navy.mil 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 12-29-2004 07:49 AM

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

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

 Brian Beck 12-29-2004 10:45 AM

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

seberino@spawar.navy.mil 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 12-29-2004 01:35 PM

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

Marius Bernklev wrote:

> * seberino@spawar.navy.mil
>
>
>>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 12-29-2004 02:05 PM

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

<seberino@spawar.navy.mil> wrote in message
> 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 12-29-2004 03:33 PM

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

On 2004-12-29, seberino@spawar.navy.mil <seberino@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".

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

 Roy Smith 12-29-2004 04:08 PM

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

In article <41d2ce43\$0\$35731\$a1866201@visi.com>,
Grant Edwards <grante@visi.com> wrote:

> On 2004-12-29, seberino@spawar.navy.mil <seberino@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).

All times are GMT. The time now is 10:17 AM.