python基礎4_1-裝飾器(decorator)

定義:
本質是函數(裝飾其他函數),就是爲其他函數添加附加功能。

原則:

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

先看一個完整的裝飾器的例子:

# Author: Mr.Xue
# 2019.10.23

"""
實現裝飾的知識儲備:
	1、函數即‘變量’
	2、高階函數
	3、嵌套函數

高階函數+嵌套函數--->裝飾器
"""

import time

def timmer(func):
	def warpper(*args, **kwargs):
		start_time = time.time()
		func()
		stop_time = time.time()
		print('the func run time is %s' % (stop_time - start_time))
	return warpper

@timmer
def test1():
	time.sleep(3)
	print("in the func test1")

test1()

1、函數即“變量”
函數在內存中的存儲機制:在內存中存儲函數體,函數名作爲門牌號
在這裏插入圖片描述
注:
python中的回收機制:不管是函數還是變量,一但"門牌號"不存在了,內存空間就會被回收

2、高階函數
符合下面兩個條件任何一個的就是高階函數:

  1. 把一個函數名作爲實參傳給另一個函數
  2. 返回值中包含函數名
'''
第一種高階函數:
把一個函數名作爲實參傳給另一個函數

--> 實現了不修改被裝飾的函數bar代碼的情況下爲其添加新功能
'''

import time

def bar():
	time.sleep(3)
	print('in the bar')

def test(func):
	start_time = time.time()
	print(func)
	func() # run function bar
	stop_time = time.time()
	print("the func run time is %s" %(stop_time-start_time))

test(bar)

#x = bar # 函數即'變量'
#x()
'''
第二種高階函數:
返回值中包含函數名

--> 不修改函數的調用方式
'''

import time

def bar():
	time.sleep(3)
	print('in the bar')

def test2(func):
	print(func) # func的內存地址
	return func #返回func的內存地址

print(test2(bar))
bar = test2(bar)
bar()

3、嵌套函數
在一個函數的函數體中還有一個用def聲明函數

def foo():
	print("in the foo")
	def bar():
		print("in the bar")
	
	bar()

foo()

好啦,理解了上面的三個知識點之後,我們來一步步寫一個裝飾器,首先我們先定義兩個函數test1、test2,再給兩個函數添加新的功能,計算這兩個函數的運行時間,怎麼做呢,先定義一個高階函數(上面提到的第一種高階函數),將這兩個函數當作參數傳進去。


import time

def deco(func):
	start_time = time.time()
	func() # run function bar
	stop_time = time.time()
	print("the func run time is %s" %(stop_time-start_time))

def test1():
	time.sleep(3)
	print('in the test1')

def test2():
	time.sleep(3)
	print('in the test2')

deco(test1)
deco(test2)

現在我們完成了裝飾器的原則之一,再不修改源代碼的基礎上給函數添加了新的功能。

看上去好像不難呀,接下來,只要我們改進代碼,完成不修改函數的調用方式的這個原則,是不是就完成了呢,好,那我們將上面的高階函數改寫成第二種的高階函數


import time

def deco(func):
	start_time = time.time()
	return func # 返回func函數的內存地址
	stop_time = time.time()
	print("the func run time is %s" %(stop_time-start_time))

def test1():
	time.sleep(3)
	print('in the test1')

def test2():
	time.sleep(3)
	print('in the test2')

test1 = deco(test1)
test2 = deco(test2)
test1() #沒有修改調用方式,新功能卻不見了
test2() #沒有修改調用方式

利用了第二種高階函數之後,我們完成了裝飾器的另外一個原則,沒有改變函數的調用方式,但是發現新功能卻沒有加上去,這時候只用高階函數來實現裝飾器好像有點困難啊,怎麼辦呢?好像進入死循環了呀。
不知道大家注意到沒有,在上面那個裝飾器的例子裏面,有一行寫着,“高階函數+嵌套函數—>裝飾器”,哦,那是不是我們再搞個嵌套函數就可以了呢,按照這個思路,我們繼續往下走。


import time
def timer(func):
	def deco():
		start_time = time.time()
		func() # 調用func函數
		stop_time = time.time()
		print("the func run time is %s" %(stop_time-start_time))
	return deco

def test1():
	time.sleep(3)
	print('in the test1')

def test2():
	time.sleep(3)
	print('in the test2')

test1 = timer(test1)
test2 = timer(test2)
test1() #沒有修改調用方式,新功能也加上去了
test2() #沒有修改調用方式,新功能也加上去了

哇哦,現在就已經完成了裝飾器了,只不過調用方式看上去比較lower而已,只要修改成標準寫法就ok了。

怎麼做呢,在要被裝飾的函數頭部,加上"@裝飾器名字",就好啦。


import time
def timer(func):
	def deco():
		start_time = time.time()
		func() # 調用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')

@timer
def test2():
	time.sleep(3)
	print('in the test2')

test1()
test2() 

只是現在我們寫的這個裝飾器和上面那個還有一點區別,我們的裝飾器沒有參數,而上面的那個卻有,接下來,我們繼續完善完善。
給我們的內嵌函數加上兩個非固定參數,這樣就可以接收參數了,如下:


import time
def timer(func):
	def deco(*args, **kwargs):
		start_time = time.time()
		res = func(*args, **kwargs) # 調用func函數
		stop_time = time.time()
		print("the func run time is %s" %(stop_time-start_time))
		return res
	return deco

@timer # test1 = timer(test1)
def test1():
	time.sleep(3)
	print('in the test1')

@timer
def test2(name, age): # 假如函數有參數怎麼辦
	time.sleep(3)
	print('in the test2', name, age)

test1()
test2("xue", 24) 

ok, all finish!

現在我們用裝飾器寫一個網站頁面的需求:比如說,一共20個網站,其中除了首頁不需要驗證可以直接顯示外,其他的都需要驗證用戶名密碼纔可以顯示。

# Author: Mr.Xue
# 2019.10.24

import time
user, passwd = 'xue', 'abc123'

def auth(auth_type):
	print("auth_type: ", auth_type)
	def outer_warpper(func):
		def warpper(*args, **kwargs):
			print("warpper func args:", *args, **kwargs)
			if auth_type == 'local':
				username = input("Username: ")
				password = input("Password: ")
				if user == username and passwd == password:
					print("\033[32;1mUser has passed authentication\033[0m")
					res = func(*args, **kwargs)
					return res
				else:
					print("\033[31;1mInvalid username or password\033[0m")	
			elif auth_type == 'ldap':
				print('bu hui gao mao xian')
		return warpper
	return outer_warpper

def index():
	print("in the index")

@auth(auth_type='local')	#home = warpper(home) -> warpper
def home():
	print("in the home")
	return 'from home' # 假如函數有返回類型怎麼辦

@auth(auth_type='ldap')
def bbs():
	print('in the bbs')

index()
print(home())
bbs()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章