Tuple passed to function recognised as string

M

Mike314

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?

Thanks.
 
M

MRAB

Mike314 said:
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,
}.
 
T

Terry Reedy

Mike314 said:
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.
"
 
A

Albert Hopkins

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?

You could have verified this simply by getting rid of the function
altogether:
--> <type 'str'>

Hint 1:--> <type 'int'>

Hint 2: It's not the parentheses that define a tuple, it's the comma(s)--> <type 'tuple'>
 
R

R. David Murray

Mike314 said:
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 in Python the syntactic element that defines something
as a tuple is the comma, not the parenthesis:
<type 'tuple'>

In your function call the comma would normally be an argument
separator, so in that context you do need the parenthesis as well:

test_func(val=('val1',))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,202
Latest member
ClaudioVil

Latest Threads

Top