[Python-talk] Reflections on tuples, September 2005, 2 of 2
Bill Sconce
sconce at in-spec-inc.com
Mon Sep 26 14:38:20 EDT 2005
On Fri, 23 Sep 2005 22:41:43 -0400
Python <python at venix.com> wrote:
> I think trailing commas are tolerated rather than encouraged.
Agreed. By you, by me, by the official language description, as
well as by actual practice.
> Only the single element tuple is forced to have a trailing comma.
> Allowing a trailing comma makes it easier to extend sequence later.
Yes. Especially if the sequence spans lexical lines, e.g.
famous_first_words = (
'Four score and seven years ago',
'When in the course of human events',
'Friends, Romans, countrymen',
)
> ... http://python.org/doc/2.3.5/ref/exprlists.html (*)
> Just being nit-picky, but if you are dealing with a class, it is
> sometimes easier when you are precisely right.
I agree. We should never hide what is precisely right, nor fail to
point out how to find out what is precisely right. That means that
we need to portray things like this correctly. Perhaps:
"This tuple/comma business seems to give EVERYONE trouble, at the
beginning and later on too. Approaching it in the following way
will help you be confused less often than most people while you
learn and use Python(**), although it's only 95% accurate".(***)
Thanks for the reminder, Lloyd.
-Bill
(*) Python Reference Manual, Section 5.12, Expression Lists:
An expression list containing at least one comma yields a
tuple. The length of the tuple is the number of expressions
in the list. The expressions are evaluated from left to right.
The trailing comma is required only to create a single
tuple (a.k.a. a singleton); it is optional in all other cases.
A single expression without a trailing comma doesn't create a
tuple, but rather yields the value of that expression. (To
create an empty tuple, use an empty pair of parentheses: ().)
(Note that parentheses are mentioned, but only in the last
sentence.)
(**) "Most people" includes me - I still stumble over parentheses.
(***) Viewing it this way leaves the empty tuple as the exceptional
case. I think that will be easier for students than teaching
them parentheses as I have been doing, and then helping them
discern tuples in expressions such as these:
a, b = b, a
breakfast = 'spam', 'catsup'
meat, vegetable = breakfast
My old way made exceptions of these everyday cases, turning
things on their head. What I'd been saying here was that the
parentheses were optional. 100% accurate under the letter of the
law, perhaps, but impermissible under the spirit, since it plants
the seeds for stumbles.
> On Fri, 2005-09-23 at 18:00 -0400, Bill Sconce wrote:
> > A real clarification! ... We were discussing how parentheses
> > seem to have two different, confusing uses ... Lloyd said, almost
> > in passing "No, it's not parentheses which signify a tuple. It's
> > COMMAS."
More information about the Python-talk
mailing list