Python基礎(六) 裝飾器

2022-06-17 13:00:16 字數 4795 閱讀 5427

def foo():

print('我的函式名作為引數傳給高階函式')

def gao_jie1(func):

print('我就是高階函式1,我接收的引數名是%s' %func)

func()

def gao_jie2(func):

print('我就是高階函式2,我的返回值是%s' %func)

return func

gao_jie1(foo)

gao_jie2(foo)

1)將函式名傳遞給高階函式

import time

def foo():

time.sleep(3)

print('你好')

def test(func):

# print(func)

start_time=time.time()

func()

stop_time = time.time()

print('函式執行時間是 %s' % (stop_time-start_time))

test(foo)

你好函式執行時間是 3.0009939670562744

#我們確實為函式foo增加了foo執行時間的功能,但是foo原來的執行方式是foo(),現在我們需要呼叫高階函式test(foo),改變了函式的呼叫方式

2)高階函式返回的是乙個函式名

#沒有改變函式的呼叫方式,但多執行了一次,不合格

import time

def foo():

time.sleep(3)

print('你好')

def test(func):

# print(func)

start_time=time.time()

func()

stop_time = time.time()

print('函式執行時間是 %s' % (stop_time-start_time))

return func

foo = test(foo) #此時已經呼叫了foo,下面又呼叫了一次

foo()

你好函式執行時間是 3.000460386276245

你好

3)沒有修改被修飾函式的源**,也沒有修改被修飾函式的呼叫方式,但是也沒有為被修飾函式新增新功能

import time

def foo():

time.sleep(3)

print('來自foo')

def timer(func):

start_time=time.time()

return func

stop_time = time.time()

print('函式執行時間是 %s' % (stop_time-start_time))

foo=timer(foo)

foo()

來自foo

1)函式接收的引數是乙個函式名

作用:在不修改函式源**的前提下,為函式新增新功能,

不足:會改變函式的呼叫方式

2)函式的返回值是乙個函式名

作用:不修改函式的呼叫方式

不足:不能新增新功能

def father(name):

print('from father %s' %name)

def son():

print('from son')

def grandson():

print('from grandson')

grandson()

son()

father('aaa')

def father(auth_type):

# print('from father %s' %name)

def son():

# name='linhaifeng_1'

# print('我的爸爸是%s' %name)

def grandson():

print('我的爺爺是%s' %auth_type)

grandson()

# print(locals())

son()

# father('a')

father('filedb')

#我的爺爺是filedb

本質上是函式,功能是為其他han書新增新功能

import time

def timmer(func): #func=test

# print(func)

start_time=time.time()

func() #就是在執行test()

stop_time = time.time()

print('執行時間是%s' %(stop_time-start_time))

@timmer #test=timmer(test)

def test():

time.sleep(3)

print('test函式執行完畢')

test()

# @timmer 就相當於 test=timmer(test)

import time

def timmer(func): #func=test

start_time=time.time()

res=func() #就是在執行test()

stop_time = time.time()

print('執行時間是%s' %(stop_time-start_time))

return res

@timmer #test=timmer(test)

def test():

time.sleep(3)

print('test函式執行完畢')

return '這是test的返回值'

print(res)

執行時間是3.000176191329956

這是test的返回值

import time

def timmer(func): #func=test1

start_time=time.time()

res=func(*args,**kwargs) #就是在執行test()

stop_time = time.time()

print('執行時間是%s' %(stop_time-start_time))

return res

@timmer #test=timmer(test)

def test1(name,age,gender):

time.sleep(1)

print('test1函式執行完畢,名字是【%s】 年齡是【%s】 性別【%s】' %(name,age,gender))

return '這是test的返回值'

test1('aa',18,'male')

user_list=[,,

,,

]current_dic=

def auth(auth_type='filedb'):

def auth_func(func):

print('認證型別是',auth_type)

if auth_type == 'filedb':

if current_dic['username'] and current_dic['login']:

res = func(*args, **kwargs)

return res

username=input('使用者名稱:').strip()

passwd=input('密碼:').strip()

for user_dic in user_list:

if username == user_dic['name'] and passwd == user_dic['passwd']:

current_dic['username']=username

current_dic['login']=true

res = func(*args, **kwargs)

return res

else:

print('使用者名稱或者密碼錯誤')

elif auth_type == 'ldap':

print('鬼才特麼會玩')

res = func(*args, **kwargs)

return res

else:

print('鬼才知道你用的什麼認證方式')

res = func(*args, **kwargs)

return res

return auth_func

@auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了乙個auth_type --->index=auth_func(index)

def index():

print('歡迎來到京東主頁')

@auth(auth_type='ldap')

def home(name):

print('歡迎回家%s' %name)

#@auth(auth_type='sssssss')

def shopping_car(name):

print('%s的購物車裡有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃'))

# print('before-->',current_dic)

# index()

# print('after--->',current_dic)

# home('產品經理')

shopping_car('產品經理')

python基礎 (六) 裝飾器

裝飾器即函式,裝飾就是修飾,為其他函式新增新功能。1.不修改被裝飾函式的源 開放封閉原則 2.為被裝飾函式新增新功能後,不修改被修飾函式的呼叫方式 裝飾器 高階函式 函式巢狀 閉包 1.函式接收的引數是乙個函式名 2.函式返回值是乙個函式名 3.滿足上述條件任意乙個,都可稱之為高階函式 高階函式示例...

函式基礎(六) 裝飾器

知道京東吧 不知道?那你知道 蘑菇街吧 我們身為使用者,在進入介面的時候 首先會提示我們登陸是吧 當我們登陸的時候,接下來的所有操作就不用再驗證身份了 否則,一到收藏啊,關注啊,就需要我們重新登陸 那我們可不可以做乙個這個呢?沒有資料庫,我們模擬乙個資料庫,懂我意思吧!db def login if...

python基礎 裝飾器

裝飾器本質就是函式,功能是為其他函式新增附加功能。原則 不修改被修飾函式的源 不修改被修飾函式的呼叫方式 裝飾器的知識儲備 裝飾器 高階函式 函式巢狀 閉包 import time 定義乙個裝飾器計算函式執行時間 def timer func start time time.time res fun...