I am learning about complex function in python.
I understand the input to the complex function complex(a, b) and will return a+ bi. What I don't understand is when I input in tuples form, complex((a, b)), it will return an error but if I use complex(*(a, b)) it will return a+ bi. so in this case, is the asterisk particularly part of complex function's argument when giving it as a tuple or is it performing some kind of operation? Thanks.
The *
-operator is used for argument unpacking.
You're not calling the function in the same way. Think of it this way:
complex(real=a, imaginary=b)
# vs.
complex(real=(a, b))
This is also why you see people write def fn(*args, **kwargs)
, it means that all arguments and keyword arguments will be unpacked.