Nov-30-2023, 02:40 PM
I can unpack a dict with the ** operator. But something strange happens if I try to unpack it with *. This may be obvious given that the * operator unpacks a tuple or list. But watch this:
x = {'a': 7, 'b': 8}
print(*x)
That works and the output is "a b" but this does not:
z = *x
print(z)
So, in the first case when print is called it seems the
x = {'a': 7, 'b': 8}
print(*x)
That works and the output is "a b" but this does not:
z = *x
print(z)
So, in the first case when print is called it seems the
def print(*args)
has the effect of **x but how and why if the second example does not work? Why doesn't the code first execute *x and then pass the result to print()? If it could do that then why doesn't the second example work?