I am wondering why my comparison returns False and not True although 'a' == 'a'.
def test(*values):
return values[0]=='a'
tuple = ('a',)
test(tuple)
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
> def print_args(*args):
print(args)
> print_args('a', 'b', 'c')
# Outputs:
('a', 'b', 'c')
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
def test(values):
return values[0] == 'a'
tuple = ('a',)
test(tuple)
# Outputs:
True