日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python从入门到精通:Python装饰器详解

發布時間:2023/12/2 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python从入门到精通:Python装饰器详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

裝飾器本質就是函數,作用是裝飾其它函數,給其它函數增加附加功能,提高代碼復用,減少代碼量。

我們平時給函數增加新的功能時,通過修改此函數或在函數里調用新的函數實現,但是1、如果這個函數已經是上線的功能,這時修改程序原代碼有很大風險 2、如果有100個這樣的函數,我們就要找到100個地方進行修改。
例如:我們想新增功能,驗證函數執行了多長時間,代碼如下:
#修改原函數

import time def sum1():start = time.clock()sum = 1+2print(sum)end = time.clock()print("time used:",end - start) sum1()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py 3 time used: 2.9439962316848234e-05Process finished with exit code 0#原函數內調用新功能的函數 import time def sum1():sum = 1+ 2print (sum) def timeit(func):start = time.clock()func()end =time.clock()print("time used:", end - start) timeit(sum1)"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py 3 time used: 3.071996067845033e-05Process finished with exit code 0

裝飾器原則:

1、不能修改被裝飾函數的源代碼
2、不能修改被裝飾函數的調用方式

實現裝飾器儲備知識:

高階函數+嵌套函數=裝飾器
1、函數即變量

def test1():print('hello!')return test1 test2=test1 test2() print(test2)"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py hello! <function test1 at 0x0000018E5D1A8F28>Process finished with exit code 0

2、高階函數
a:把一個函數名當做實參傳給另外一個函數(在不修改被裝飾函數源代碼的情況下為其添加功能)
b:返回值中包含函數名
滿足a或b就是高階函數

def bar():print('in the bar') def test1(func):print(func)func()return test1 test1(bar) #bar的內存地址+調用bar函數 print(test1)#test1的內存地址 print(test1(bar))#bar的內存地址+調用bar函數+test1的內存地址"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py <function bar at 0x0000026AC7C58F28> in the bar<function test1 at 0x0000026AC82C89D8><function bar at 0x0000026AC7C58F28> in the bar <function test1 at 0x0000026AC82C89D8>Process finished with exit code 0 import time def bar():time.sleep(3)print('in the bar') def test1(func):start_time=time.time()func()stop_time=time.time()print('the func run time is %s'%(stop_time-start_time))return func bar=test1(bar) bar()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py in the bar the func run time is 3.0008511543273926 in the barProcess finished with exit code 0

3、嵌套函數

def foo():print('in the foo')def bar():print('in the bar')bar() foo()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py in the foo in the barProcess finished with exit code 0

不帶參數的裝飾器:

import time def timer(func):def deco():start_time=time.time()func()stop_time=time.time()print('the func run time is %s'%(stop_time-start_time))return deco @timer #test1=timer(test1) def test1():time.sleep(3)print('in the test1') test1()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py in the test1 the func run time is 3.000833511352539Process finished with exit code 0

帶參數的裝飾器:
從實:1中看出@timer相當于test2=timer(test2),timer(func)中func傳的是test2,故func=test2
timer(test2)=deco,因為test2=timer(test2),故test2=deco=func
test2(name,age)=deco(name,age)=func(name,age)所以傳參到deco和func里
實例1:

import time def timer(func):def deco(*args,**kwargs):start_time=time.time()func(*args,**kwargs)stop_time=time.time()print('the func run time is %s'%(stop_time-start_time))return deco @timer #test1=timer(test1) def test1():time.sleep(3)print('in the test1') test1() @timer #test2=timer(test2) def test2(name,age):time.sleep(3)print('%s %s in the test2'%(name,age)) test2('wangli',22)"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py in the test1 the func run time is 3.0001492500305176 wangli 22 in the test2 the func run time is 3.000540256500244Process finished with exit code 0

實例2:

import timedef timmer(flag):""":param flag: 接收裝飾器的參數:return:"""def outer_wrapper(func):""":param func: 接收被裝飾的函數:return:"""# 接收被裝飾函數的參數def wrapper(*args, **kwargs):""":param args: 收集被裝飾函數的參數:param kwargs: 收集被裝飾函數的關鍵字參數:return:"""if flag == "true":start_time = time.time()# 調用被裝飾的函數result = func(*args, **kwargs)# 讓進程睡一秒time.sleep(1)stop_time = time.time()print("{func} spend {time} ".format(func="add", time=stop_time - start_time))return resultelse:print("Unexpected ending")return wrapperreturn outer_wrapper

總結

以上是生活随笔為你收集整理的Python从入门到精通:Python装饰器详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。