I'm a bit confused. Let's create a function called x. I know that by putting * before the y, this means that we can add as many arguments as we want.
def x(*y):
return y
However.
Case 1:
>>> x(1, 2)
(1, 2)
Case 2:
Let's pass a list [1,2] with an asterisk before it:
>>> x(*[1,2])
(1, 2)
It seems that the single asterisk has two uses:
For allowing multiple arguments in a function - essentially putting them into a list
If done twice, to "break apart" a list into separate items
Why is this? Why can't I do something like: *a*b?
解决方案
In function definition * is used to collect all positional arguments in a tuple, in a function call * unpacks an iterable and passes it's items as positional arguments.
If by *a*b you're trying to unpack two iterables/iterators a and b then correct way is:
>>> a = [1, 2, 4]
>>> b = 'foo'
>>> from itertools import chain
def func(*x):
print x
...
>>> func(*chain(a,b)) #chain will work for both iterators and iterables
(1, 2, 4, 'f', 'o', 'o')
if both a and b are of same type and are iterable then you can also use :
>>> a = [1, 2, 4]
>>> b = [0,1]
>>> func(*(a + b))
(1, 2, 4, 0, 1)