How do I pass variable length arguments to parameters of another function?
def fun(a,b,c):
#calculating
pass
from inspect import signature
def example(*args):
if len(args) >= len(signature(fun).parameters):
fun(args); # how do i do this without modifying fun function
return
print("error")
Change fun(args)
to fun(*args[:3])
. This takes only the first 3 elements of args
and unpacks them.
Edit: As kindall writes in his answer, you should also parameterise numargs
.
Use slicing to make sure you have the right number of parameters, then use unpacking to pass them as individual arguments.
def example(*args):
numargs = len(signature(fun).parameters)
if len(args) >= numargs:
return fun(*args[:numargs])
print("not enough arguments")
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.