length of a tuple or a list containing only one element

Glenn Linderman v+python at g.nevcal.com
Thu Jan 8 00:28:14 EST 2009


On approximately 11/3/2008 11:55 AM, came the following characters from 
the keyboard of Tim Chase:
>>>> 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
>
>
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>

I'm glad you guys had this thread, which I read with interest and some 
amusement back when it happened.  I am an experienced programmer, but a 
relative newcomer to Python.

And so it was, the last couple nights, that I spent much time looking 
for why my CherryPy configuration didn't work, and after much searching 
of the CherryPy documentation for a tracing technique (which I still 
wish I could find), I finally hacked the code to add an extra pprint.  
Even after that, I focused on the wrong data in the pprint output.

At long last, I discovered that somehow my hash-of-hashes was mostly a 
hash-of-hashes, but there was a tuple in there that contained a hash 
too!  Now how did that get in there?

conf['/path'] = {
    'item1': 'value1',
    'item2': 'value2',
    },

So I was focusing on the items and values of the pprint, and they were 
all correct.  But this tuple clearly didn't belong, but my brain was 
expecting that tuples would be surrounded by () in source...

-- 
Glenn -- http://nevcal.com/
===========================
A protocol is complete when there is nothing left to remove.
-- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking




More information about the Python-list mailing list