调用方法,那这个时候咱们就可使用装饰器来实现修改python
# 原函数 def name(): print('hello xichen') name # 为原函数增长功能 # 修改源代码的方式 def name(): print('---------') print('hello xichen') print('---------') name()
import time def index(): print('welcome to index') time.sleep(1) def func(func): start = time.time() fun() end = time.time() print(start-end) func(index)
# 没有返回值的 import time def index(): print('welcome to index') time.sleep(1) def func(fun): def f():# 从新建立的index start = time.time() fun() # 真正的index end = time.time() print(start-end) return f index = func(index) # index = func(),其实就是等于f() index()
# 有返回值的 import time def index(): print('welcome to index') time.sleep(1) return 123 def func(fun): def f():# 从新建立的index start = time.time() res = fun() # 真正的index end = time.time() print(start-end) return res return f index = func(index) # index = fun,其实就是等于f() res = index() print(res)
import time def func(fun): def f(): start = time.time() res = fun() end = time.time() print(end - start) return res return f def index(): print('hello xichen') time.sleep(1) return 123 index = func(index) res = index() print(res)
import time def func(fun): def f1(*args, **kwargs): print('args:', args) print('kwargs:', kwargs) start = time.time() res = fun(*args, **kwargs) end = time.time() print(end - start) return res return f1 @func def index(a,b=1): print('a:', a) print('b', b) print('hello xichen') time.sleep(1) return 123 res = index(1) print(res)
@装饰器名
import time def func(fun): def f(*args, **kwargs): print('args:', args) print('kwargs:', kwargs) start = time.time() res = fun(*args, **kwargs) end = time.time() print(end - start) return res return f @func def index(a,b=1): print('a:', a) print('b', b) print('hello xichen') time.sleep(1) return 123 res = index(1) print(res)
def func(fun): def f(*args,**kwargs): res = fun(*args,**kwargs) # 给原来的函数加功能 return res return f
def data(enter): def func(fun): def f(*args,**kwargs): if enter == 'file': uname = input('uname:') upwd = input('upwd:') if uname == 'xichen' and upwd == '123': print('登录成功') res = fun(*args,**kwargs) return res else: print('登陆失败') else: print('数据来源数据库,不可用') return f return func @data('file') def shopping(): print('欢迎shopping') return 123 res = shopping() print(res)
def demo2(func2): def add_func2(*args,**kwargs): print('-------------') func2(*args,**kwargs) # func2此时是demo1装饰的函数add,添加的功能的add_func1 print('-------------') return func2 return add_func2 def demo1(func1): def add_func(*args,**kwargs): print('***********') func1(*args,**kwargs) # func1是被demo装饰的函数add print('***********') return func1 return add_func def add(): print('hello') # 先运行的是先装饰的函数demo1 add = demo1(add) # add = func1,也就是在调用add_func1,add返回的是add_func1 add = demo2(add) # add = func2,也就是在调用add_func2,add返回的是add_func2 add()
def demo2(fun2): def add_func2(*args,**kwargs): res = fun2(*args,**kwargs) # 添加功能 return res return add_fun2 def demo1(fun1): def add_func1(*args,**kwargs): res = fun1(*args,**kwargs) # 添加功能 return res return add_fun1 def add(): print('hello xichen') return 123 add = demo1(add) add = demo2(add) add()
def demo(enter): def func(fun): def f(*args,**kwargs) # 添加功能 res = fun(*args,**kwargs) return f return func @demo(参数---enter) def add() print('hello xichen') return 123 res = add() res()
func
,可是三层的装饰器解除了这个限制。咱们不单单可使用单个参数的三层装饰器,多个参数的只须要在三层装饰器中多加入几个参数便可。也就是说装饰器三层便可,多加一层反倒无用。