这是一个简单程序的大纲。
我一直在收到"global name foo is not defined"的错误提示......谁能帮忙解决一下吗?谢谢!
# some pre-defined constants
A = 1
B = 2
# function that does something critical
def foo(num1, num2):
# do something
# main program.... do something to A and B
for i in range(20):
# do something to A and B
# and update A and B during each iteration
import timeit
t = timeit.Timer(stmt="foo(num1,num2)")
print t.timeit(5)
我一直在收到"global name foo is not defined"的错误提示......谁能帮忙解决一下吗?谢谢!
foo
被定义在不同于t
的作用域中... - senderleduration,return_value= timeit.Timer(functools.partial(your_function_name, argument_1, argument_2,argument_3)).timeit(1)
,其中your_function_name
是foo
,如果你编写了一个函数如下:def foo(argument_1, argument_2, argument_3):
。同时包括 @Hugh Bothwell 的timeit.template = """..
块。return_value
是函数返回的值,例如argument_1 + argument_2
。使用import timeit, functools
。此解决方案由 @Jose Ricardo Bustos M. 提供。 - a.t.