Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically pass unknown number of parameters in Python

Tags:

python

I'd like an easy way to plug in a function and arguments to be executed later so I can have a kind of wrapper around it.

In this case, I'd like to use it to benchmark, example:

def timefunc(fn, *args):
   start = time.clock()
   fn(args)
   stop = time.clock()
   return stop - start

How do I pass arguments where the number of parameters aren't known? How do I find the number of parameters? How would I even call the function once I knew the number of parameters?

like image 870
emcee Avatar asked Mar 13 '26 03:03

emcee


1 Answers

Just call the function like this:

fn(*args)
like image 78
Tyler Avatar answered Mar 15 '26 03:03

Tyler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!