zoukankan      html  css  js  c++  java
  • day14

    函数对象与闭包
      函数对象:

       函数对象指的是函数可以被当做’数据’来处理,函数对象的本质是是一个变量

      # 1、可以赋值
      # f=func
      # print(f,func)
      # f()

      # 2、可以当做函数当做参数传给另外一个函数
      # 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']()
      函数的嵌套:

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

        # 1、函数的嵌套调用:在调用一个函数的过程中又调用其他函数
        # def max2(x,y):
        # if x > y:
        # return x
        # else:
        # return y
        #
        #
        # def max4(a,b,c,d):
        # # 第一步:比较a,b得到res1
        # res1=max2(a,b)
        # # 第二步:比较res1,c得到res2
        # res2=max2(res1,c)
        # # 第三步:比较res2,d得到res3
        # res3=max2(res2,d)
        # return res3
        #
        # res=max4(1,2,3,4)
        # print(res)


        # 2、函数的嵌套定义:在函数内定义其他函数
        # def f1():
        # def f2():
        # pass


        # 圆形
        # 求圆形的求周长:2*pi*radius
        def circle(radius,action=0):
         from math import pi

         def perimiter(radius):
         return 2*pi*radius

         # 求圆形的求面积:pi*(radius**2)
         def area(radius):
         return pi*(radius**2)

         if action == 0:
         return 2*pi*radius

         elif action == 1:
         return area(radius)

        circle(33,action=0)
      闭包函数:

        

    一:大前提:
    闭包函数=名称空间与作用域+函数嵌套+函数对象
    核心点:名字的查找关系是以函数定义阶段为
    二:什么是闭包函数
    "闭"函数指的该函数是内嵌函数
    "包"函数指的该函数包含对外层函数作用域名字的引用(不是对全局作用域
    闭包函数:名称空间与作用域的应用+函数嵌套
    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()
    装饰器  
      1.1 什么是装饰器
        器指的是工具,可以定义成函数

        装饰:为其他事物添加额外的东西点缀    

        装饰器指的是定义一个函数,该函数是用来为其他函数添加额外的功能

      1.2 为什么要用装饰器
        开放封闭原则:对扩展开放,对修改封闭(对扩展功能开放,对修改源代码封闭)

        装饰器就是在不修改被装饰器对象的源代码以及调用方式的前提下为被装饰对象添加新功能

      1.3 无参装饰器的实现    
        # 需求:在不修改index函数的源代码以及调用方式的前提下为其添加统计运行时间的功能
        # def index(x,y):
        # time.sleep(3)
        # print('index %s %s' %(x,y))
        #
        # index(111,222)
        # # index(y=111,x=222)
        # # index(111,y=222)

        # 解决方案一:失败
        # 问题:没有修改被装饰对象的调用方式,但是修改了其源代码
        # import time
        #
        # def index(x,y):
        # start=time.time()
        # time.sleep(3)
        # print('index %s %s' %(x,y))
        # stop = time.time()
        # print(stop - start)
        #
        # index(111,222)


        # 解决方案二:失败
        # 问题:没有修改被装饰对象的调用方式,也没有修改了其源代码,并且加上了新功能
        # 但是代码冗余
        # import time
        #
        # def index(x,y):
        # time.sleep(3)
        # print('index %s %s' %(x,y))
        #
        # start=time.time()
        # index(111,222)
        # stop=time.time()
        # print(stop - start)
        #
        #
        #
        # start=time.time()
        # index(111,222)
        # stop=time.time()
        # print(stop - start)
        #
        #
        # start=time.time()
        # index(111,222)
        # stop=time.time()
        # print(stop - start)


        # 解决方案三:失败
        # 问题:解决了方案二代码冗余问题,但带来一个新问题即函数的调用方式改变了
        # import time
        #
        # def index(x,y):
        # time.sleep(3)
        # print('index %s %s' %(x,y))
        #
        # def wrapper():
        # start=time.time()
        # index(111,222)
        # stop=time.time()
        # print(stop - start)
        #
        # wrapper()

        # 方案三的优化一:将index的参数写活了
        # import time
        #
        # def index(x,y,z):
        # time.sleep(3)
        # print('index %s %s %s' %(x,y,z))
        #
        # def wrapper(*args,**kwargs):
        # start=time.time()
        # index(*args,**kwargs) # index(3333,z=5555,y=44444)
        # stop=time.time()
        # print(stop - start)
        #
        # # wrapper(3333,4444,5555)
        # # wrapper(3333,z=5555,y=44444)


        # 方案三的优化二:在优化一的基础上把被装饰对象写活了,原来只能装饰index
        # import time
        #
        # def index(x,y,z):
        # time.sleep(3)
        # print('index %s %s %s' %(x,y,z))
        #
        # def home(name):
        # time.sleep(2)
        # print('welcome %s to home page' %name)
        #
        #
        # def outter(func):
        # # func = index的内存地址
        # def wrapper(*args,**kwargs):
        # start=time.time()
        # func(*args,**kwargs) # index的内存地址()
        # stop=time.time()
        # print(stop - start)
        # return wrapper
        #
        # index=outter(index) # index=wrapper的内存地址
        # home=outter(home) # home=wrapper的内存地址
        #
        #
        # home('egon')
        # # home(name='egon')

        # 方案三的优化三:将wrapper做的跟被装饰对象一模一样,以假乱真
        # import time
        #
        # def index(x,y,z):
        # time.sleep(3)
        # print('index %s %s %s' %(x,y,z))
        #
        # def home(name):
        # time.sleep(2)
        # print('welcome %s to home page' %name)
        #
        # def outter(func):
        # def wrapper(*args,**kwargs):
        # start=time.time()
        # res=func(*args,**kwargs)
        # stop=time.time()
        # print(stop - start)
        # return res
        #
        #
        #
        # return wrapper
        # # 偷梁换柱:home这个名字指向的wrapper函数的内存地址
        # home=outter(home)
        #
        #
        # res=home('egon') # res=wrapper('egon')
        # print('返回值--》',res)
        大方向:如何在方案三的基础上不改变函数的调用方式
      
      1.4 语法
        

        import time

        # 装饰器
        # def timmer(func):
        # def wrapper(*args,**kwargs):
        # start=time.time()
        # res=func(*args,**kwargs)
        # stop=time.time()
        # print(stop - start)
        # return res
        #
        #
        #
        # return wrapper
        #
        #
        在被装饰对象正上方的单独一行写@装饰器名字
        # # @timmer # index=timmer(index)
        # def index(x,y,z):
        # time.sleep(3)
        # print('index %s %s %s' %(x,y,z))
        #
        # # @timmer # home=timmer(ome)
        # def home(name):
        # time.sleep(2)
        # print('welcome %s to home page' %name)
        #
        #
        # index(x=1,y=2,z=3)
        # home('egon')

  • 相关阅读:
    Tomcat server.xml中配置的connectionTimeout参数无效?客户端等待超时时间
    org.joda.time.datetime.plusMonths 添加月数,缺少天数。
    微信小程序苹果手机请求地址 报错404,安卓可以正常请求
    在Ubuntu 18.04 Desktop图形中配置静态和动态IP
    Ubuntu16.04设置静态IP或动态ip(DHCP)
    linux监控命令
    lsof命令详解
    通过程序名称kill掉所有的进程
    linux目录和Windows目录对比
    Linux系统下查找安装包所在目录
  • 原文地址:https://www.cnblogs.com/hansblogs/p/13355992.html
Copyright © 2011-2022 走看看