[Python-3000] a slight change to __[get|set|del]item__

Michael Chermside mcherm at mcherm.com
Mon Jun 5 15:52:07 CEST 2006

Tomer writes:
> well is func((1,2,3)) the same as func(1,2,3)? no.
> so why should container[1, 2, 3] be the same as container[(1,2,3)]?
> you say it's a feature. is it intentionally *ambiguous*?
> what you'd want in that case is
>     t = (1, 2, 3)
>     container[*t]
> or something like that.
> i guess it's a dead subject, but i wanted to have that clarified.

There's no ambiguity, the rule is like this:

Parentheses are a piece of syntax that is used for grouping
everywhere *except* in function/method argument lists (both
function declarations and invocations). Empty parentheses are also
used to indicate an empty tuple. The comma is a piece of synatax
that has special meaning in function declarations, function/method
invocations, list literals, and dictionary literals (I think
that's the full list of exceptions). Everywhere else it indicates
tuple creation.

Admitedly, it's slightly odd that a special exception to the
meaning of parentheses is made for the syntax of functions, but
there is a LONG and powerful historical convention that makes this
the most widely accepted syntax for function invocation. Both
Smalltalk and Lisp were brilliant languages whose popularity was
(IMO) severely wounded by failing to maintain this syntax for
function invocation.

Using the comma to separate items in collections makes good sense
too. List and dictionary literals obviously fall into this
category. Making tuple be "the collection with no syntax" was a
clever syntactical trick that allows things like these:

     return a, b
     x, y = y, x
     for i, x in enumerate(aList):

to feel completely natural yet still be regular in syntax.

-- Michael Chermside

More information about the Python-3000 mailing list