I am wondering why my comparison returns False
and not True
although 'a' == 'a'
.
JavaScript
x
6
1
def test(*values):
2
return values[0]=='a'
3
4
tuple = ('a',)
5
test(tuple)
6
Output: False
Advertisement
Answer
It’s because you are using *values
rather than values
in your function definition
When you use the special syntax *args
in a function, args
will already come back as a tuple, where each arg is an element of the tuple.
So for example
JavaScript
1
7
1
> def print_args(*args):
2
print(args)
3
> print_args('a', 'b', 'c')
4
5
# Outputs:
6
('a', 'b', 'c')
7
In your case since you are passing in a tuple already, w/in the function values
is like “Ok, I’ll happily take a tuple as my first argument”, and values
becomes a tuple of tuples (well a tuple of a single tuple). Thus you are comparing (‘a’,) to ‘a’ and your check fails
TL;DR: either pass in just 'a'
or change *values
to values
JavaScript
1
9
1
def test(values):
2
return values[0] == 'a'
3
4
tuple = ('a',)
5
test(tuple)
6
7
# Outputs:
8
True
9