装饰器基本装饰器12345678910import timedef timer(func): start_time = time.time() end_time = time.time() func() print(end_time-start_time) @timer def foo(): print('foo') 被装饰函数带参数123456789101112import timedef timer(func): def inner(*args, **kwargs): start_time = time.time() end_time = time.time() func(*args, **kwargs) print(end_time-start_time) return inner@timerdef run(name, age): print('running', name) 装饰器函数带参数1234567891011121314import timedef timer(user_message): print(user_message) def outer(func): def inner(*args, **kwargs): start_time = time.time() end_time = time.time() func(*args, **kwargs) print(end_time-start_time) return inner return outer@timer("user_message")def run(name, age): print('running', name) 被装饰函数带返回值1234567891011121314151617import timedef timer(user_message): print(user_message) def outer(func): def inner(*args, **kwargs): start_time = time.time() end_time = time.time() temp = func(*args, **kwargs) print(end_time-start_time) return temp return inner return outer@timer("user_message")def run(name, age): print('running', name)print(run('name', 'age'))