length of a tuple or a list containing only one element

Tim Chase python.list at tim.thechases.com
Mon Nov 3 14:55:25 EST 2008


>>> For making a literal tuple, parentheses are irrelevant; only the
>>> commas matter:
>> I don't think I'd go so far as to say that the parentheses around tuples 
>> are *irrelevant*...maybe just relevant in select contexts
>>
>>  >>> def foo(*args):
>>  ...     for i, arg in enumerate(args):
>>  ...             print i, arg
>>  ...
>>  >>> foo(1,2)
>>  0 1
>>  1 2
>>  >>> foo((1,2))  # these parens are pretty important :)
>>  0 (1, 2)
>>
>> pedantically-grinning-ducktyping-and-running-ly yers,
> 
> I'll see your pedantry and raise you one:
>  >>> foo()
>  >>> foo(())
> 0 ()

And just because another "tuples without parens" case exists:

  >>> foo(,)
    File "<stdin>", line 1
      foo(,)
          ^
  SyntaxError: invalid syntax

To maintain the poker theme, I'd say "You raised, and I call" but 
my call fails :-P

-tkc






More information about the Python-list mailing list