Why tuple with one item is no tuple

Bill Mill bill.mill at gmail.com
Tue Mar 15 14:05:53 EST 2005


On Tue, 15 Mar 2005 10:47:28 -0800, James Stroud <jstroud at mbi.ucla.edu> wrote:
> On Tuesday 15 March 2005 08:25 am, Roy Smith wrote:
> > a = ()       # tuple of zero elements
> > a = (1,)     # tuple of one element
> > a = 1,       # tuple of one element
> > a = (1)      # scalar
> > a = (1, 2)   # tuple of two elements
> > a = 1, 2     # tuple of two elements
> > a = ,        # syntax error
> >
> > The big question is, is it the parens that make it a tuple, or is it
> > the comma?  If you go along with the parens school of thought, then
> > (1,) is the special case.  If you believe in commas, then the () is
> > the special case.  In either case, it's a bit ugly, but we learn to
> > overlook the occasional cosmetic blemishes of those we love :-)
> 
> The answer is obvious, the naked comma should be an empty tuple.
> 

The other answer, that parens should be required to surround all
tuples, is obvious too.

Neither is particularly appealing; a lone comma creating a data
structure seems counter-intuitive, but it's nice to do a, b = b, a
instead of (a, b) = (b, a) . In this case, since the need to create
empty tuples is vanishingly rare, I'm okay with a little
inconsistency.

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list