調用方法,那這個時候咱們就可使用裝飾器來實現修改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
,可是三層的裝飾器解除了這個限制。咱們不單單可使用單個參數的三層裝飾器,多個參數的只須要在三層裝飾器中多加入幾個參數便可。也就是說裝飾器三層便可,多加一層反倒無用。