風雨征程——閉包函數、裝飾器、裝飾器修正,第二十天

太晚了,我就隨便寫點,畢竟今天講課了。


作用域關係在函數定義階段時就已經固定死了,與調用位置無關

即:在任意位置調用函數都需要跑到定義函數時尋找作用域關係

def f1():
    x=1
    def inner():
        print(x)

    return inner

func=f1()

def f2():
    x=111111
    func()k

f2()

閉包函數:

閉是指該函數是一個內部函數

包是指該函數包含對外部作用(非全局作用域)名字的引用

def outter():
    x = 1
    def inner():
        print(x)

    return inner

f=outter()

def f2():
    x=1111111
    f()

f2()


def f3():
    x=4444444444444
    f()

f3()

爲函數傳值的方式之一:使用參數的形式

def inner(x):
    print(x)

inner(1)
inner(1)
inner(1)

爲函數的傳值的方式之二:包給函數

def outter(x):
    # x=1
    def inner():
        print(x)
    return inner

f=outter(1)
f()
import requests

def get(url):
    response=requests.get(url)
    if response.status_code == 200:
        print(response.text)

get('https://www.baidu.com')
get('https://www.baidu.com')
get('https://www.baidu.com')

get('https://www.python.org')
get('https://www.python.org')
get('https://www.python.org')
get('https://www.python.org')



import requests

def outter(url):
    # url='https://www.baidu.com'
    def get():
        response=requests.get(url)
        if response.status_code == 200:
            print(response.text)
    return get

baidu=outter('https://www.baidu.com')
python=outter('https://www.python.org')


baidu()
baidu()

python()
python()


裝飾器:

1.什麼是裝飾器

器指的是工具,而程序中的函數具備某一功能的工具

裝飾指的是給被裝飾的對象添加額外功能

就目前知識點來看:

定義裝飾器就是定義一個函數,只不過該函數的功能是用來爲其他函數添加額外的功能

其實:

裝飾器本身其實可以是任意可調用的對象,被裝飾的對象可以是任意可調用的對象

2.爲什麼要用裝飾器

軟件的維護應該遵循開放封閉原則

開放封閉原則指的是:

軟件一旦上線運行後對修改源代碼是封閉的,對擴展功能是開放的

這就用到了裝飾器


裝飾器的實現必須遵循兩大原則:

1.不修改被裝飾對象的源代碼

2.不修改被裝飾對象的調用方式

裝飾器其實就在遵循1和2原則的前提下爲被裝飾對象添加新功能


3.如何用裝飾器

import time

def index():
    start=time.time()
    print('welcom to index')
    time.sleep(3)
    stop=time.time()
    print('run time is %s' %(stop-start))
index()

import time

def index():
    print('welcom to index')
    time.sleep(3)

def f2():
    print('from f2')
    time.sleep(2)

start=time.time()
index()
stop=time.time()
print('run time is %s' %(stop-start))

start=time.time()
f2()
stop=time.time()
print('run time is %s' %(stop-start))









import time

def index():
    print('welcom to index')
    time.sleep(3)

def timmer(func):
    start=time.time()
    func()
    stop=time.time()
    print('run time is %s' %(stop-start))

timmer(index)
import time

def index():
    print('welcom to index')
    time.sleep(3)

def timmer(func): #func=最原始的index
    # func=index
    def inner():
        start=time.time()
        func()
        stop=time.time()
        print('run time is %s' %(stop-start))
    return inner

f=timmer(index)
f()

index=timmer(被裝飾函數的內存地址)
index=timmer(index) #index=inner

index() #inner()
import time

def index():
    print('welcom to index')
    time.sleep(3)

def timmer(func):
    #func=最原始的index
    def wrapper():
        start=time.time()
        func()
        stop=time.time()
        print('run time is %s' %(stop - start))
    return wrapper

index=timmer(index) #index=wrapper函數的內存地址
index()

3.裝飾器修正

import time

def index():
    print('welcome to index')
    time.sleep(3)
    return 123

def home(name):
    print('welcome %s to home page' %name)
    time.sleep(2)


def timmer(func):
    #func=最原始的index
    def wrapper(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print('run time is %s' %(stop - start))
        return res
    return wrapper
#
#
index=timmer(index)
home=timmer(home)

res=index()
home('egon')
裝飾器語法

裝飾器語法,

在被裝飾器對象正上方,並且是單獨一行寫上@裝飾器名

import time
def timmer(func):
    #func=最原始的index
    def wrapper(*args,**kwargs):
        start=time.time()
        res=func(*args,**kwargs)
        stop=time.time()
        print('run time is %s' %(stop - start))
        return res
    return wrapper

@timmer # index=timmer(index)
def index():
    print('welcome to index')
    time.sleep(3)
    return 123

@timmer # home=timmer(home)
def home(name):
    print('welcome %s to home page' %name)
    time.sleep(2)

res=index()
home('egon')
def deco(func):
    def wrapper(*args,**kwargs):
        res=func(*args,**kwargs)
        return res
    return wrapper

發佈了43 篇原創文章 · 獲贊 8 · 訪問量 8788
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章