Tuple passed to function recognised as string
MRAB
google at mrabarnett.plus.com
Wed Mar 18 20:07:39 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?
>
It's the comma that makes the tuple, except for one special case: the
empty tuple "()". The parentheses are there just for clarity and
operator precedence.
('val1') is not a 1-tuple, but just parentheses for grouping. The
1-tuple is ('val1', ). Notice that a trailing comma is allowed in tuples
as well as list and dict literals, eg (1, 2, ), [1, 2, ], {1: 2, 3: 4,
}.
More information about the Python-list
mailing list