Why tuple with one item is no tuple
reinhold-birkenfeld-nospam at wolke7.net
Tue Mar 15 20:34:44 CET 2005
Bill Mill wrote:
> 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
And if you don't like it at all, you can still use tuple() to "create"
an empty tuple.
More information about the Python-list