tuple syntax ',' (ending in comma?)

Georg Brandl g.brandl-nospam at gmx.net
Tue Apr 4 09:19:26 EDT 2006


Michael Yanowitz wrote:
> Hello:
> 
>    I am still relatively new to Python. I am confused by the syntax for
> tuples.
> I had:
>   thread.start_new_thread(read_data_thread, (strDataFilename))
>   and got back the following error:
> 
>   File "scene.py", line 256, in readData
>     thread.start_new_thread(read_data_thread, (strDataFilename))
> TypeError: 2nd arg must be a tuple
> 
>    The way I fixed this error was I added an extra , (comma) to the tuple:
>   thread.start_new_thread(read_data_thread, (strDataFilename,))
> 
>   I am just confused by the syntax. I am used to a comma meaning that there
> should be another parameter after the comma and if no additional parameter
> the comma would not be necessary.

What would you expect

>>> 3 * (5 + 2)

to print? Certainly not "(7, 7, 7)", but it would if all expressions in
parentheses were tuples.

Thus, the comma is necessary to disambiguate and explicitly tell the parser
that you mean to construct a tuple.

Georg



More information about the Python-list mailing list