Reputation: 41
In the following function, args[i]
should unpack into the arguments of the function func via the *
before it, however what gets passed in is a list. What am I missing?
def mymap(func, *seq):
args = list(zip(seq))
ret = []
for i in range(len(args)):
print(type(args[i]))
ret.append( func(*args[i]) )
return ret
f = lambda x: x+2
mymap(f, [1,2,3])
Upvotes: 2
Views: 820
Reputation: 85442
The *
packs in a function definition and unpacks in a function call.
Defining a new function:
def func1(*args):
print(args)
this packs:
>>> func1(1, 2)
(1, 2)
A function with two parameters
def func2(a, b):
print(a)
print(b)
can be called with a sequence using the *
:
>>> func2(*[1, 2])
1
2
Upvotes: 5