zoukankan      html  css  js  c++  java
  • python之闭包函数及相关内容

    一、函数对象

     精髓:可以把函数当成变量去用

     1、可以赋值

    def func():
        print('from func')
    
    f=func
    print(f,func)
    f()
    

     2、可以当做函数当做参数传给另外一个函数

    def func():
        print('from func')
    
    def foo(x): # x = func的内存地址
    
        # print(x)
    
        x()
    
    foo(func) # foo(func的内存地址)
    

     3、可以当做函数当做另外一个函数的返回值

    def foo(x):  # x=func的内存地址
        return x  # return func的内存地址
    
    res=foo(func)  # foo(func的内存地址)
    print(res)  # res=func的内存地址
    
    res()
    

     4、可以当做容器类型的一个元素

    l=[func,]
    
    # print(l)
    
    l[0]()
    
    dic={'k1':func}
    print(dic)
    dic['k1']()
    

     函数对象应用示范:

    def login():
        print('登录功能')
    
    def transfer():
        print('转账功能')
    
    def check_banlance():
        print('查询余额')
    
    def withdraw():
        print('提现')
    
    def register():
        print('注册')
    
    func_dic={
        '1':login,
        '2':transfer,
        '3':check_banlance,
        '4':withdraw,
        '5':register
    }
    
    # func_dic['1']()
    
    while True:
        print("""
        0 退出
        1 登录
        2 转账
        3 查询余额
        4 提现
        5 注册
        """)
        choice = input('请输入命令编号:').strip()
        if not choice.isdigit():
            print('必须输入编号,傻叉')
            continue
    
        if choice == '0':
            break
    
        if choice in func_dic:
            func_dic[choice]()
        else:
            print('输入的指令不存在')
    
        # if choice == '1':
        #     login()
        # elif choice == '2':
        #     transfer()
        # elif choice == '3':
        #     check_banlance()
        # elif choice == '4':
        #     withdraw()
        # else:
        #     print('输入的指令不存在')
    

    二、函数嵌套

    1、函数的嵌套调用:在调用一个函数的过程中又调用其他函数

    def max2(x,y):
        if x > y:
            return x
        else:
            return y
    
    def max4(a,b,c,d):
        res1=max2(a,b)   # 第一步:比较a,b得到res1
        res2=max2(res1,c)  # 第二步:比较res1,c得到res2
        res3=max2(res2,d)  # 第三步:比较res2,d得到res3
        return res3
    
    res=max4(1,2,3,4)
    print(res)
    

    2、函数的嵌套定义:在函数内定义其他函数

    def f1():
        def f2():
            pass
    
    
    # 圆形
    def circle(radius,action=0):
        from math import pi
    
        def perimiter(radius):  # 求圆形的求周长:2*pi*radius
            return 2*pi*radius
    
        def area(radius):  # 求圆形的求面积:pi*(radius**2)
            return pi*(radius**2)
    
        if action == 0:
            return 2*pi*radius
    
        elif action == 1:
            return area(radius)
    
    circle(33,action=0)
    

    三、闭包函数

    1、大前提:
      闭包函数=名称空间与作用域+函数嵌套+函数对象
      核心点:名字的查找关系是以函数定义阶段为准

    2、什么是闭包函数
      "闭"函数指的该函数是内嵌函数
      "包"函数指的该函数包含对外层函数作用域名字的引用(不是对全局作用域)

    闭包函数:名称空间与作用域的应用+函数嵌套

    def f1():
        x = 33333333333333333333
        def f2():
            print(x)
        f2()
    
    x=11111
    def bar():
        x=444444
        f1()
    
    def foo():
        x=2222
        bar()
    
    foo()
    

    闭包函数:函数对象

    def f1():
        x = 33333333333333333333
        def f2():
            print('函数f2:',x)
        return f2
    
    f=f1()
    
    # print(f)
    
    # x=4444
    
    # f()
    
    def foo():
        x=5555
        f()
    
    foo()
    
    
    

    三:为何要有闭包函数--------->闭包函数的应用
    两种为函数体传参的方式
    方式一:直接把函数体需要的参数定义成形参

    def f2(x):
        print(x)
    
    f2(1)
    f2(2)
    f2(3)
    

    方式二:

    def f1(x): # x=3
        x=3
        def f2():
            print(x)
        return f2
    
    x=f1(3)
    print(x)
    
    x()
    

    案例:已简单的爬虫,爬网页数据为例

    import requests
    
    # 传参的方案一:
     def get(url):
         response=requests.get(url)
         print(len(response.text))
    
     get('https://www.baidu.com')
     get('https://www.cnblogs.com/linhaifeng')
     get('https://zhuanlan.zhihu.com/p/109056932')
    
    
    # 传参的方案二:
    def outter(url):
        # url='https://www.baidu.com'
        def get():
            response=requests.get(url)
            print(len(response.text))
        return get
    
    baidu=outter('https://www.baidu.com')
    baidu()
    
    cnblogs=outter('https://www.cnblogs.com/linhaifeng')
    cnblogs()
    
    zhihu=outter('https://zhuanlan.zhihu.com/p/109056932')
    zhihu()
    
  • 相关阅读:
    mysql服务设置远程连接 解决1251 client does not support ..问题
    Docker的简单使用
    Kick Start 2018
    Kick Start 2018
    Kick Start 2018
    LeetCode——三维形体的表面积
    面试金典——按摩师
    LeetCode——使数组唯一的最小增量
    LeetCode——单词接龙 II
    LeetCode——N皇后 II
  • 原文地址:https://www.cnblogs.com/Lance-WJ/p/12534196.html
Copyright © 2011-2022 走看看