python之闭包,装饰器

函数对象 :相当于变量名

函数对象的作用:

1. 可以引用

def f1():
    print("from f1")

a = f1
a()

'''
from f1

'''

2. 可以作为函数的返回值

def f1():
    print("from f1")

def f2():
    return f1

a = f2()
a()

'''
from  f1
'''

3. 可以作为容器的元素

def f1():
    print("from f1")
a = [1,2,3,4,5,f1]

4. 可以作为函数的参数

def f1():
    print("from f1")

def f2(func):
    func()

f2(f1)

'''
from f1
'''

闭包

定义:

python中的闭包从表现形式上定义(解释)为:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure)

闭包的意义:

返回的函数对象,不仅仅是一个函数对象,在该函数外还包裹了一层作用域,这使得该函数无论在何处调用,优先使用自己外层包裹的作用域

# 闭包
def test_plf(x):

    def test_lt():
        print(x)
    return test_lt

f1 = test_plf(8)
f1()


# 闭包的应用
import requests
def req(url):
    def test_plf():
        request = requests.get(url)
        data = request.status_code
        print(data)
    return test_plf

baidu = req("https://www.baidu.com")
baidu()
baidu()
baidu()
boke = req("https://www.cnblogs.com/plf-Jack/")
boke()
boke()
boke()


# 使用普通函数进行对比
import requests
def req(url):
    content = requests.get(url)
    data = content.status_code
    print(data)

req("https://www.baidu.com")
req("https://www.baidu.com")
req("https://www.baidu.com")
req("https://www.cnblogs.com/plf-Jack/")
req("https://www.cnblogs.com/plf-Jack/")
req("https://www.cnblogs.com/plf-Jack/")

'''
    总结:
        相比使用普通函数和使用闭包函数实现相同业务逻辑
        1. 闭包在调用上要比普通函数要简单
        2. 闭包相对于普通函数的定义,逻辑上难度要大一些
        3. 闭包主要应用于延迟计算,爬虫领域
'''

装饰器

本质:装饰器 = 函数。它的本质就是函数

作用:给原函数增加新的功能

原则:

1. 不能改变被装饰的函数的调用方式

2. 装饰器不能修改装饰函数的源代码

实现装饰器的知识储备:

1. 函数即“变量”

2. 高阶函数。即一个函数可以作为另外一个函数的参数,这种函数就称之为高阶函数。

3. 嵌套函数。即定义一个函数时,在其函数体中又定义了一个函数,这个函数称之为嵌套函数。

装饰器的语法

def deco(func):
    def wrapper(*args,**kwargs):
        res = func(*args,**kwargs)
        return res
    return wrapper
  1. 无参装饰器

    import time
    def test_lt(func):
        def warpper():
            start_time = time.time()
            func()
            end_time = time.time()
            print("你睡了%s秒"%(end_time-start_time))
        return warpper
    
    @test_lt   # 语法糖,即简洁。它等同于 test_plf = test_lt(test_plf)
    def test_plf():
        time.sleep(1)
        print("我睡了多长时间")
  2. 返回值装饰器

    import time
    
    def test_lt(func):
        def warpper():
            start_time = time.time()
            res = func()
            end_time = time.time()
            print("睡了%s秒"%(end_time-start_time))
            return res
        return warpper
    
    @test_lt  #test_plf = test_lt(test_plf)
    def test_plf():
        time.sleep(1)
        print("我睡了多长时间")
        return 123
    
    a = test_plf()
    print("返回值为%d"%a)
  3. 有参装饰器

    dic = {"PLF":'123'}
    is_login = False
    def auth(test_type='file'):
        def login_desc(func):
            def wrepper():
                if test_type == "file":
                    global is_login
                    if not is_login:
                        user_name = input("请输入账号>>>")
                        user_pwd = input("请输入密码>>>")
                        if dic.get(user_name) != user_pwd:
                            print("输入错误")
                            return
                        print("密码输入正确")
                        is_login = True
                        func()
                        return
                    func()
                elif test_type == "txt":
                    print("文件类型不正确")
                else:
                    print("你走开")
            return wrepper
        return login_desc
    
    
    @auth(test_type="file")
    def go_shopping():
        print("购物")
    
    @auth(test_type="txt")
    def pay():
        print("支付")
    
    @auth(test_type="makeDown")
    def see_list():
        print("查看清单")
    
    go_shopping()
    pay()
    see_list()

猜你喜欢

转载自www.cnblogs.com/plf-Jack/p/10957486.html