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])
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.