Tuple passed to function recognised as string

Terry Reedy tjreedy at udel.edu
Wed Mar 18 20:09:43 EDT 2009


Mike314 wrote:
> Hello,
> 
>    I have following code:
> 
> def test_func(val):
>     print type(val)
> 
> test_func(val=('val1'))
> test_func(val=('val1', 'val2'))
> 
> The output is quite different:
> 
> <type 'str'>
> <type 'tuple'>
> 
> Why I have string in the first case?

Because (<any expression>) == <any expression>.
Perhaps you meant ('val1',).

"Parenthesized forms
A parenthesized form is an optional expression list enclosed in parentheses:

parenth_form ::=  "(" [expression_list] ")"

A parenthesized expression list yields whatever that expression list 
yields: if the list contains at least one comma, it yields a tuple; 
otherwise, it yields the single expression that makes up the expression 
list.

An empty pair of parentheses yields an empty tuple object.
"




More information about the Python-list mailing list