风雨征程——闭包函数、装饰器、装饰器修正,第二十天

太晚了,我就随便写点,毕竟今天讲课了。


作用域关系在函数定义阶段时就已经固定死了,与调用位置无关

即:在任意位置调用函数都需要跑到定义函数时寻找作用域关系

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值