包装函数没问题:How do I use line_profiler (from Robert Kern)?
from line_profiler import LineProfiler
import random
def do_stuff(numbers):
s = sum(numbers)
l = [numbers[i]/43 for i in range(len(numbers))]
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()
但是,我似乎无法弄清楚这是将相同的技术应用于方法。
假设我尝试在方法上使用相同的技术:
class Foo:
def method(self):
return 1
obj = Foo()
lp = LineProfiler()
lp_wrapper = lp(method)
obj.lp_wrapper() # Causes an error since Foo does not have a method called "lp_wrapper"
解决此错误的最佳方法是什么?谢谢。
答案 0 :(得分:0)
最好的答案来自schwobaseggl:“您尝试过lp_wrapper = lp(obj.method)
”
事实证明,这就是包装方法的方式。