Python隨心記 函式閉包為函式加上認證功能

2022-06-23 03:09:11 字數 4016 閱讀 1946

函式閉包為函式加上認證功能
def

auth_func(func):

user_name = input('

使用者名稱:

').strip()

user_pass = input('

密 碼:

').strip()

if user_name == '

achun

'and user_pass == 77770000:

ret = func(*args,**kwargs)

return

ret

else

:

print('

賬戶或密碼錯誤!')

return

@auth_func

defindex():

print('

歡迎來到京東')

@auth_func

defhome(name):

print('

歡迎%s回家

' %name)

@auth_func

defshopping_car(name,names,namess):

print('

購物車的東西【%s,%s,%s】

' %(name,names,namess))

index()

home(

'achun')

shopping_car('奶茶

','籃球

','褲子

')

函式閉包模擬session  目前知識:全域性變數

user_list =[

, #

這裡注意資料型別

,]current_dic=

defauth_func(func):

if current_dic['

user

'] and current_dic['

login']:

ret = func(*args, **kwargs)

return

ret username = input('

使用者名稱:

').strip()

userpass = input('

密 碼:

').strip()

for user_dic in

user_list:

if username == user_dic['

user

'] and userpass == user_dic['

pass']:

ret = func(*args,**kwargs)

current_dic[

'user

'] =username

current_dic[

'login

'] =true

return

ret

else:#

若整個迴圈都未找到對應的賬戶和密碼將會退出

print('

賬戶或密碼錯誤!')

return

@auth_func

defindex():

print('

歡迎來到京東')

@auth_func

defhome(name):

print('

歡迎%s回家

' %name)

@auth_func

defshopping_car(name,names,namess):

print('

購物車的東西【%s,%s,%s】

' %(name,names,namess))

index()

home(

'achun')

shopping_car('奶茶

','籃球

','褲子

')

函式閉包帶引數裝飾器  新增應用場景

user_list =[

, #

這裡注意資料型別

,]current_dic=

def auth( auth_type = '

filedb

'): #

可以傳乙個場景

defauth_func(func):

#print('場景是%s' %auth_type)

if auth_type == '

filedb':

if current_dic['

user

'] and current_dic['

login']:

ret = func(*args, **kwargs)

return

ret username = input('

使用者名稱:

').strip()

userpass = input('

密 碼:

').strip()

for user_dic in

user_list:

if username == user_dic['

user

'] and userpass == user_dic['

pass']:

ret = func(*args, **kwargs)

current_dic[

'user

'] =username

current_dic[

'login

'] =true

return

ret

else: #

若整個迴圈都未找到對應的賬戶和密碼將會退出

print('

賬戶或密碼錯誤!')

elif auth_type == '

mongodb':

print('

這裡執行另外的操作')

else

:

print('

這裡執行另外的操作')

return

return

auth_func

@auth(auth_type = '

filedb

') #

auth_func = auth(auth_type = 'filedb')

defindex():

print('

歡迎來到京東')

@auth(auth_type = '

mongodb

') #

auth_func = auth(auth_type = 'filedb')

defhome(name):

print('

歡迎%s回家

' %name)

@auth(auth_type = '

redis

') #

auth_func = auth(auth_type = 'filedb')

defshopping_car(name,names,namess):

print('

購物車的東西【%s,%s,%s】

' %(name,names,namess))

@auth(auth_type = '

memcached

') #

auth_func = auth(auth_type = 'filedb')

defdetailed_list(name,names,namess):

print('

購物車的東西【%s,%s,%s】

' %(name,names,namess))

index()

home(

'achun')

shopping_car('奶茶

','籃球

','褲子

')

Python隨心記 module模組和包

import sysprint sys.path 列印python直譯器的路由 新增路徑 臨時修改 module模組和包 packge,該檔案下有個 init py檔案 import module module 引入模組 模組一共三種 python標準庫模組 第三方模組 應用程式自定義模組 引入自定...

python閉包函式

python函式閉包 closure 比較抽象,在函式式程式設計中運用地比較多,通俗點就是子函式 內嵌函式 呼叫上層函式 閉包函式 的變數,且上層函式 閉包函式 接收的變數會儲存在子函式 內嵌函式 的變數中可以供子函式 內嵌函式 呼叫 概念很抽象,但是實現的例子還是比較容易理解的,這裡記住實現函式閉...

python閉包函式

python是一種物件導向的程式語言,在python中一切皆物件,這樣就使得變數所擁有的屬性,函式也同樣擁有。這樣我們就可以理解在函式內建立乙個函式的行為是完全合法的。這種函式被叫做內嵌函式,這種函式只可以在外部函式的作用域內被正常呼叫,在外部函式的作用域之外呼叫會報錯。而如果內部函式裡引用了外部函...