国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中裝飾器學習總結

Python中裝飾器學習總結

2021-01-14 00:27ITxiaoke Python

這篇文章主要介紹了Python中裝飾器學習總結,分享了相關代碼示例,小覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文研究的主要內容是Python裝飾器相關學習總結,具體如下。

裝飾器(decorator)功能

  • 引入日志
  • 函數執行時間統計
  • 執行函數前預備處理
  • 執行函數后清理功能
  • 權限校驗等場景
  • 緩存

裝飾器示例

例1:無參數的函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from time import ctime, sleep
 
def timefun(func):
 def wrappedfunc():
  print("%s called at %s"%(func.__name__, ctime()))
  func()
 return wrappedfunc
 
@timefun
def foo():
 print("I am foo")
 
foo()
sleep(2)
foo()

分析如下:

上面代碼理解裝飾器執行行為可理解成

foo = timefun(foo)

1,foo先作為參數賦值給func后,foo接收指向timefun返回的wrappedfunc
2,調用foo(),即等價調用wrappedfunc()
3,內部函數wrappedfunc被引用,所以外部函數的func變量(自由變量)并沒有釋放
4,func里保存的是原foo函數對象

例2:被裝飾的函數有參數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from time import ctime, sleep
 
def timefun(func):
 def wrappedfunc(a, b):
  print("%s called at %s"%(func.__name__, ctime()))
  print(a, b)
  func(a, b)
 return wrappedfunc
 
@timefun
def foo(a, b):
 print(a+b)
 
foo(3,5)
sleep(2)
foo(2,4)

例3:被裝飾的函數有不定長參數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from time import ctime, sleep
 
def timefun(func):
 def wrappedfunc(*args, **kwargs):
  print("%s called at %s"%(func.__name__, ctime()))
  func(*args, **kwargs)
 return wrappedfunc
 
@timefun
def foo(a, b, c):
 print(a+b+c)
 
foo(3,5,7)
sleep(2)
foo(2,4,9)

例4:裝飾器中的return

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from time import ctime, sleep
 
def timefun(func):
 def wrappedfunc():
  print("%s called at %s"%(func.__name__, ctime()))
  func()
 return wrappedfunc
 
@timefun
def foo():
 print("I am foo")
 
@timefun
def getInfo():
 return '----hahah---'
 
foo()
sleep(2)
foo()
 
 
print(getInfo())

執行結果:

foo called at Sun Jun 18 00:31:53 2017
I am foo
foo called at Sun Jun 18 00:31:55 2017
I am foo
getInfo called at Sun Jun 18 00:31:55 2017
None

如果修改裝飾器為return func(),則運行結果:

foo called at Sun Jun 18 00:34:12 2017
I am foo
foo called at Sun Jun 18 00:34:14 2017
I am foo
getInfo called at Sun Jun 18 00:34:14 2017
----hahah---

小結:一般情況下為了讓裝飾器更通用,可以有return

例5:裝飾器帶參數,在原有裝飾器的基礎上,設置外部變量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from time import ctime, sleep
 
def timefun_arg(pre="hello"):
 def timefun(func):
  def wrappedfunc():
   print("%s called at %s %s"%(func.__name__, ctime(), pre))
   return func()
  return wrappedfunc
 return timefun
 
@timefun_arg("itcast")
def foo():
 print("I am foo")
 
@timefun_arg("python")
def too():
 print("I am too")
 
foo()
sleep(2)
foo()
 
too()
sleep(2)
too()
可以理解為
 
foo()==timefun_arg("itcast")(foo)()

例6:類裝飾器

裝飾器函數其實是這樣一個接口約束,它必須接受一個callable對象作為參數,然后返回一個callable對象。在Python中一般callable對象都是函數,但也有例外。只要某個對象重寫了 call() 方法,那么這個對象就是callable的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Test():
 def __call__(self):
  print('call me!')
 
t = Test()
t() # call me
類裝飾器demo
 
 
class Test(object):
 def __init__(self, func):
  print("---初始化---")
  print("func name is %s"%func.__name__)
  self.__func = func
 def __call__(self):
  print("---裝飾器中的功能---")
  self.__func()

說明:

1. 當用Test來裝作裝飾器對test函數進行裝飾的時候,首先會創建Test的實例對象,并且會把test這個函數名當做參數傳遞到init方法中
即在init方法中的func變量指向了test函數體
2. test函數相當于指向了用Test創建出來的實例對象
3. 當在使用test()進行調用時,就相當于讓這個對象(),因此會調用這個對象的call方法
4. 為了能夠在call方法中調用原來test指向的函數體,所以在init方法中就需要一個實例屬性來保存這個函數體的引用
所以才有了self.func = func這句代碼,從而在調用__call方法中能夠調用到test之前的函數體

?
1
2
3
4
5
@Test
def test():
print(“—-test—”)
test()
showpy()#如果把這句話注釋,重新運行程序,依然會看到”–初始化–”

運行結果如下:

---初始化---
func name is test
---裝飾器中的功能---
----test---

wraps函數

使用裝飾器時,有一些細節需要被注意。例如,被裝飾后的函數其實已經是另外一個函數了(函數名等函數屬性會發生改變)。

添加后由于函數名和函數的doc發生了改變,對測試結果有一些影響,例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def note(func):
 "note function"
 def wrapper():
  "wrapper function"
  print('note something')
  return func()
 return wrapper
 
@note
def test():
 "test function"
 print('I am test')
 
test()
print(test.__doc__)

運行結果

note something
I am test
wrapper function

所以,Python的functools包中提供了一個叫wraps的裝飾器來消除這樣的副作用。例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import functools
def note(func):
 "note function"
 @functools.wraps(func)
 def wrapper():
  "wrapper function"
  print('note something')
  return func()
 return wrapper
 
@note
def test():
 "test function"
 print('I am test')
 
test()
print(test.__doc__)

運行結果

note something
I am test
test function

總結

以上就是本文關于Python中裝飾器學習總結的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/u014745194/article/details/73400158

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 看av网站| 91亚洲国产精品 | 国产精品国产成人国产三级 | 日本视频二区 | 久久久91视频 | 日韩国产精品一区二区三区 | 日韩爱爱网址 | 亚洲国产二区 | 国产在线视频一区二区 | 国产精品毛片久久久久久久av | 欧美精三区欧美精三区 | 精品国产91乱码一区二区三区 | 噜噜噜噜狠狠狠7777视频 | 激情五月激情综合网 | 亚洲精品日韩激情在线电影 | 国产一级纯肉体一级毛片 | 欧美午夜一区二区三区免费大片 | 亚洲国产精品激情在线观看 | 亚洲精品成人在线 | 精品欧美一区二区久久久伦 | 日本三级中文在线电影 | 国产综合一区二区 | 亚洲国产高清在线 | 亚洲第一免费播放区 | 亚洲欧美综合乱码精品成人网 | 亚洲视频一区二区三区在线观看 | 伦理自拍 | 91精品久久久久久久久久久久久久久 | 伦乱视频 | 淫片免费观看 | 一级片免费视频 | 欧美精品在线看 | 成人小视频在线观看 | 久久精品中文字幕 | 国产精品久久久久国产a级 国产免费久久 | 日韩欧美三级 | 一本一道久久a久久精品逆3p | 国产精品18久久久久久久久久久久 | 久久久精品456亚洲影院 | 日韩免费av | 日韩精品一区在线 |