如何在 class 方法中使用装饰器
how to use decorator in class method
如何在class方法中使用装饰器
import time
def myTimer(func, *args, **kwargs):
def wrapper():
start = time.perf_counter()
func(*args, **kwargs)
elapsed_time = time.perf_counter() - start
print(elapsed_time)
return wrapper
class Example:
def __init__(self):
pass
@myTimer
def data(self):
return [i for i in range(10000)]
e = Example()
e.data()
out_put = TypeError: wrapper() 采用 0 个位置参数,但给出了 1 个
@khelwood 先说了,但我正在尝试这个编辑
def myTimer(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
func(*args, **kwargs)
elapsed_time = time.perf_counter() - start
print(elapsed_time)
return wrapper
class Example:
def __init__(self):
pass
@myTimer
def data(self):
return [i for i in range(10000)]
e = Example()
e.data()
如何在class方法中使用装饰器
import time
def myTimer(func, *args, **kwargs):
def wrapper():
start = time.perf_counter()
func(*args, **kwargs)
elapsed_time = time.perf_counter() - start
print(elapsed_time)
return wrapper
class Example:
def __init__(self):
pass
@myTimer
def data(self):
return [i for i in range(10000)]
e = Example()
e.data()
out_put = TypeError: wrapper() 采用 0 个位置参数,但给出了 1 个
@khelwood 先说了,但我正在尝试这个编辑
def myTimer(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
func(*args, **kwargs)
elapsed_time = time.perf_counter() - start
print(elapsed_time)
return wrapper
class Example:
def __init__(self):
pass
@myTimer
def data(self):
return [i for i in range(10000)]
e = Example()
e.data()