number变量第一次被调用时值是2
number变量第二次被调用时值是5
number变量第三次被调用时值是20
number变量第四次被调用时值是18
number变量第五次被调用时值是9
一共调用5次,最终.numberr值为 20
# -*- coding: utf-8 -*- # @Time : 2022/6/6 0:06 # @Author : chuqianyu # @FileName: test00.py # @Software: PyCharm # @Blog :https://home.cnblogs.com/u/chuqianyu from functools import wraps def counter(func): @wraps(func) def tmp(*args, **kwargs): tmp.count += 1 return func(*args, **kwargs) tmp.count = 0 tmp.numberr = 0 return tmp @counter def foo(x): print('foo:'+str(2*x)) if 2*x > foo.numberr: foo.numberr = 2*x print(foo.count) print(foo.numberr) foo(1) # print(foo.count) # 1 # print(foo.numberr) # 2 foo(10) # print(foo.count) # 2 # print(foo.numberr) # 20 foo(5) # print(foo.count) # 3 # print(foo.numberr) # 30
运行结果: