Reputation: 648
Recently I noticed that when I pass to the function argument which is tuple value from dictionary it has trailing comma at the end. Bellow is the simple code example of my issue.
def myfun(*args):
print(f'args={args}')
x, y = args
print(f'x={x}, y={y}')
myfun(1, 2) # passing arguments this way works fine
arg_dict = {0: (1, 2), 1: (2, 3)}
print(f'arg_dict[0]={arg_dict[0]}') # when I print dictionary value it seems quite OK.
myfun(arg_dict[0]) # passed dictionary value has trailing comma.
Here is the output:
args=(1, 2)
x=1, y=2
arg_dict[0]=(1, 2)
args=((1, 2),)
Traceback (most recent call last):
File "c:\Users\name\Documents\pname\test.py", line 28, in <module>
myfun(arg_dict[0])
File "c:\Users\name\Documents\pname\test.py", line 21, in myfun
x, y = args
ValueError: not enough values to unpack (expected 2, got 1)
I am wondering why python interpreter decides to pack tuple from dictionary like this? I am using python3.6.
Upvotes: 0
Views: 106
Reputation: 547
You pass tuple as a first argument. You need to unpack values:
myfun(*arg_dict[0])
Upvotes: 2