閉包函式與裝飾器

2022-08-23 07:45:10 字數 4745 閱讀 6303

八.閉包函式:定義在函式內部的函式,並且該函式包含對外部函式作用中名字的引用,該函式就稱為閉包函式

閉:指的是定義在函式內部的函式

作用域關係:在函式定義階段就規定死了,與呼叫位置無關

def outter():

x=2def inner():

#x=1

print('from inner',x)

return inner

f=outter() #f=inner

f()def foo():

x=111111111 #呼叫階段賦不生效 函式體值在定義階段生效

f()foo()

應用場景:

九.無參裝飾器

裝飾器即在不修改被裝飾的物件源**與呼叫方式的前提下,為裝飾器物件的新增新功能

裝飾器與被裝飾的物件均可以是任意可呼叫的物件

裝飾器==>函式

被裝飾的物件==>函式

import time

def index():

time.sleep(3)

print('welcome to index page')

def home(name):

time.sleep(5)

print('welcome %s to home page' %name)

def outter(func): #func=最原始的index outter函式就是裝飾器

start_time=time.time()

func() #func()=index()

stop_time = time.time()

print('run time is %s' %(stop_time - start_time))

index()

有無參裝飾器之返回值

import time

def index(): #無參

time.sleep(3)

print('welcome to index page')

return 123456789

def home(name): #有參

time.sleep(5)

print('welcome %s to home page'%name)

#####################裝飾器##############

def timmer(func):

start_time = time.time()

res=func(*args,**kwargs) #最原始的index()

stop_time = time.time()

print('run time is %s'(stop_time - start_time))

return res #返回最原始的index()的返回值

index=timmer(index)

home=timmer(home)

index()

home('zhang')

裝飾器的語法:

import time

def outer(func):

def inner(*args,**kwargs): #裝飾器的引數

start_time = time.time()

res=func(*args,**kwargs) #被裝飾的函式

stop_time = time.time()

print('run time is %s'(stop_time - start_time))

return res

return inner

@outer #index=outer(index)

def index():

time.sleep(3)

print('welcome to index page')

return 123456789

@outer #home=outer(home)

def home(home)

time.sleep(5)

print('welcome %s to home page'%name)

index()

home('zhang')

#認證裝飾器案例:

import time

current_user=

def auth(func):

if current_user['username']: #none的布林值一定為假 如果裡面有值就一定為真

print('已經登入過')

res=func(*args,**kwargs) #如果已經登入過 直接返回值給res

return res

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

pwd=input('密碼》:').strip()

if username == 'zhang' and pwd = '123':

print('登入成功')

res=func(*args,**kwargs)

return res

else:

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

@auth #index=auth(index)

def index():

time.sleep(1)

print('welcome to index page')

return 122

@auth #home=auth(home)

def home(home):

time.sleep(2)

print('welcome %s to home page'%name)

index()

home('zhang')

#疊加裝飾器

import time

current_user=

def auth(func):

#func=index

if current_user['username']: #none的布林值為假 如果有值 一定為真

print('已經登入過')

res=func(*args,**kwargs)

return res

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

pwd=input("密碼》:").strip()

if username == 'egon' and pwd == '123':

print('登入成功')

current_user['username']=username

res=func(*args,**kwargs)

return res

else:

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

def timmer(func):

start_time=time.time()

res=func(*args,**kwargs)

stop_time=time.time()

print(stop_time-start_time)

return res

@timmer #統計的是auth+index的執行時間 跟裝飾器的位置有關

@auth #index=auth(index)

def index():

time.sleep(1)

print('welcome to index page')

return 122

index()

有參裝飾器:

import time

current_user=

def auth(engine):

# engine='file'

def auth2(func):

# func=index

if engine == 'file':

if current_user['username']:

print('已經登陸過了')

res=func(*args,**kwargs)

return res

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

pwd=input('密碼》: ').strip()

if uname == 'zhang' and pwd == '123':

print('登陸成功')

current_user['username']=uname

res=func(*args,**kwargs)

return res

else:

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

elif engine == 'mysql':

print('基於myql的認證')

elif engine == 'ldap':

print('基於ldap的認證')

return auth2

def index():

time.sleep(1)

print('welcome to index page')

return 122

閉包函式與裝飾器

一 閉包函式 定義在函式內部的函式,並且該函式包含對外部函式作用域中名字的引用,該函式就稱為閉包函式。乙個持有外部環境變數的函式就是閉包,閉包 函式塊 定義函式時的環境。閉包函式是乙個能記住巢狀作用域變數值的函式,儘管作用域已經不存在 工廠函式定義了乙個外部的函式,這個函式簡單的生成並返回乙個內嵌的...

閉包函式與裝飾器

目錄 三 三層裝飾器 閉是封閉 函式內部的函式 包是包含 該內部函式對外部作用域而非全域性作用域的變數的引用 閉包就是指 函式內部的函式 對外部作用域 而非全域性作用域 的引用。閉包函式的作用 可以把 閉包函式內部的變數 閉包函式內部的函式 這兩者包裹在一起,然後通過返回值的形式返回出來。閉包函式的...

裝飾器與閉包

閉包 內層函式呼叫外層函式的引數,並且返回內層函式,叫做閉包。裝飾器 是裝飾器的符號 裝飾器是對閉包的一種利用,內層函式呼叫外層函式的引數,並且返回內層函式,叫做閉包,把呼叫的引數,換成函式,就是裝飾器,因為python中,函式也是當做物件,從而有裝飾器這一種特殊的用法。有引數的裝飾器就是在外面一層...