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

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

你可能感兴趣的:(python 统计函数调用次数的装饰器)