I have a Python function called func that I am trying to time using timeit.
func takes an argument, f, that is also a function. In the program I'm writing, it's a lambda function, but it can be a regularly defined function as well. What matters is that the source code for this function is not available to func, all that's available is a reference to it.
What I'm trying to do:
def testTimingOfFunc(f):
time = timeit.timeit("package.func(f)", "from src import package")
I get an error that says that f isn't defined.
Everything else is working correctly. When I call:
testTimingOfFunc('lambda x:x**2')
and change the function to:
def testTimingOfFunc(f):
time = timeit.timeit("package.func(" + str(f) + ")",
"from src import package")
everything works as it should, so I know that everything regarding the timeit function is fine.
That's not how testTimingOfFunc works though; A function reference gets passed in, not a string.
Any ideas on how I can pass f as an argument to func in timeit?