python 统计函数调用次数的装饰器

2019-12-12 18:02:27 浏览数 (1)

import types from functools import wraps

def profiled(func): ncalls = 0 @wraps(func) def wrapper(*args, *kwargs): nonlocal ncalls ncalls = 1 return func(args, **kwargs) wrapper.ncalls = lambda: ncalls return wrapper

Example

@profiled def add(x, y): return x y

0 人点赞