Python 将函数的函数传递给timeit getting NameError

Python 将函数的函数传递给timeit getting NameError,python,Python,我想用timeit对三个类似的函数计时。我已经编写了这段代码,但我不明白在将函数传递给测试函数时发生了什么 def f0(x, y, z): #some code here def f1(x, y, z): #a slighty similar function def f2(x, y, z): #still another similar function def test(name): x=100 y=100 z=100 res=name(

我想用timeit对三个类似的函数计时。我已经编写了这段代码,但我不明白在将函数传递给测试函数时发生了什么

def f0(x,  y,  z):
#some code here

def f1(x,  y,  z):
#a slighty similar function


def f2(x, y, z):
#still another similar function



def test(name):
    x=100
    y=100
    z=100
    res=name(x,y,z)

if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test(f0)", setup="from __main__ import test"))
我得到的错误是:

NameError: global name 'f0' is not defined
您需要导入测试中提到的所有全局名称:

print(timeit.timeit("test(f0)", setup="from __main__ import test, f0"))
print(timeit.timeit("test(f0)", setup="from __main__ import test, f0"))

test(f0)
也需要查找
f0
,而不仅仅是
test()

,您还必须像这样导入
f0