python提高 高阶函数 嵌套函数 闭包 装饰器

一,装饰器的作用和原理
# def num1():
# ###########一,装饰器的作用和原理#########
#     def test():
#         print('1,装饰器本身就是函数,功能是为其他函数添加付附加功能')
#         print('2原则:不能修改被装饰函数的源代码,不能修改被装饰函数的调用方式')
#         print('3,装饰器=高阶函数+函数嵌套+闭包')
#     test()
# num1()
装饰器的作用和原理
二,嵌套函数和作用域
# def num2():
# #################二,嵌套函数和作用域############
#     # 就是函数里面还有函数
#     def test1():
#         print('form test1')
#         def test2():
#             print('form test1')
#             '.....'
#             test2()
#     test1()
# num2()
嵌套函数和作用域
三,高阶函数
# def num3():
#     ################三,高阶函数################
#     # 把函数当作参数传给另外一个函数
#     def foo(n):
#         print(n)
#     def bar(name):
#         print("my name is %s"%name)
#     foo(bar('qyl'))
#     # 函数体return 返回函数
#     def foo():
#         print('from foo')
#         return bar()#返回的是bar的返回值
#     def bar():
#         print('from bar')
#     print(foo())
#     # 尾调用:返回的函数是最后一部调用,叫尾调用
#     def bar(n):
#         print('bar',n)
#         return foo(n)
#     def foo(x):
#         print('foo',x)
#         return bar(x)+1#不是最后一步,所以不是尾调用
#     foo(10)
#     # 可以改成尾递归
#     def bar(n):
#         print('bar',n)
#         return foo(n)
#     def foo(x):
#         print('foo',x)
#         x+1
#         return bar(x)
#     foo(10)
# num3()
三,高阶函数
四,只用高阶函数,会更改调用函数
# def num4():
#     ###############四,只用高阶函数,会更改调用函数#############
#     # 高阶函数没有改变函数体,但改变了调用函数
#     import time
#     def foo():
#         time.sleep(3)
#         print("这个是原程序,不改变这个源程序,计算这个函数的时间")
#     def foo1(fucn):
#         st=time.time()
#         fucn()
#         st1 = time.time()
#         print("用这个函数装饰原函数FOO:%s"%(st1-st))
#     foo1(foo)#不过更改了原程序的调用
# num4()
只用高阶函数,会更改调用函数
五,闭包
# def num5():
# ################五,闭包#######################33
# # 闭包:就是一个函数包着另外一个函数就是把外面的变量传到里面可以用
#     def father(name):
#         def sun():
#             def groudsun():
#                 print(name)#可以找到外面传过来的变量或函数
#             groudsun()
#         sun()
#     father('qyl')
# num5()
闭包
六,装饰器
1,装饰器架子
# def test1():
#     import time
#     def timer(fucn):
    #         def bb():
    #             start_time=time.time()
    #             fucn()
    #             stop_time = time.time()
    #             print('这个程序的运行时间%s'%(stop_time-start_time))
    #         return bb
    #     def tt():
    #         time.sleep(3)
    #         print("需要装饰的函数")
    #     tt=timer(tt)
    #     tt()
    # test1()
装饰器架子
2,语法堂
def test2():
        ################2,语法堂#############
        # 用语法堂就是@timer==>tt=timer(tt)
        import time
        # 装饰器函数
        def timer(fucn):
            def bb():
                start_time=time.time()
                fucn()
                stop_time = time.time()
                print('这个程序的运行时间%s'%(stop_time-start_time))
            return bb
        @timer
        def tt():
            time.sleep(3)
            print("需要装饰的函数")
        # tt=timer(tt)#用语法堂就是@timer
        tt()
    test2()
语法堂
3,返回值
def test3():
        ################3,返回值#############
        # 现在tt()就是bb()所以返回值
        import time
        # 装饰器函数
        def timer(fucn):
            def bb():
                start_time=time.time()
                res=fucn()#这个才是tt的运行函数
                stop_time = time.time()
                print('这个程序的运行时间%s'%(stop_time-start_time))
                # return '这个是bb的返回值'
                return res#所以要返回res的值
            return bb
        @timer
        def tt():
            time.sleep(3)
            print("需要装饰的函数")
        # tt=timer(tt)#用语法堂就是@timer
            return '这个是tt的返回值'
        print(tt())
    test3()
返回值
4,传入参数
 def test4():
        ################4,传入参数#############
        #*args, ** kwargs就是转进去什么值会原封不动的给您什么值
        import time
        # 装饰器函数
        def timer(fucn):
            def bb(*args,**kwargs):
                start_time=time.time()
                res=fucn(*args,**kwargs)#这个才是tt的运行函数
                stop_time = time.time()
                print('这个程序的运行时间%s'%(stop_time-start_time))
                # return '这个是bb的返回值'
                return res#所以要返回res的值
            return bb
        @timer
        def tt(name,age):
            time.sleep(3)
            print("需要装饰的函数,名字:%s,年龄:%s"%(name,age))
        # tt=timer(tt)#用语法堂就是@timer
            return '这个是tt的返回值'
        print(tt('qyl',19))
    test4()
传入参数
5带参数,带验证
  def test5():
        ###########5带参数,带验证#############
        dic_zh = [{'name': 'qyl', 'passwd': '123'},
                  {'name': 'qjq', 'passwd': '123'},
                  {'name': 'xp', 'passwd': '123'}
                  ]
        dic_zt = {"username": None, 'loagin': False}
        def decor(use_type='fileyz'):  # 传装饰器形参
            def zhuangshiqi(fucn):
                def zsq(*args, **kwargs):
                    # 加装饰器
                    if use_type == 'fileyz':
                        if dic_zt["username"] and dic_zt['loagin']:
                            res = fucn(*args, **kwargs)
                            return res
                        zh = input('请输入账号:').strip()
                        mm = input('请输入密码:').strip()
                        for z in dic_zh:
                            if zh == z['name'] and z['passwd'] == mm:
                                dic_zt['username'] = zh
                                dic_zt['loagin'] = True
                                res = fucn(*args, **kwargs)
                                return res
                        else:
                            print("账号不正确")
                    elif use_type == 'abd':
                        print('运行别的形式')
                return zsq
            return zhuangshiqi
        @decor(use_type='abd')  #-->zhuangshiqi=decor(use_type='abd')-->@zhuangshiqi-->
        def jiamulu(i):
            print('欢迎%s进入家目录' % i)
        @decor(use_type='fileyz')
        def zhujiemian(i):
            print('欢迎%s进入主界面' % i)
        jiamulu('qyl')
        zhujiemian('qyl')
    test5()
带参数,带验证

 

posted @ 2022-05-24 09:06  quyulin  阅读(30)  评论(0)    收藏  举报