zoukankan      html  css  js  c++  java
  • 第一类对象-> 函数名 -> 变量名

    函数对象对象可以像变量一样进行赋值
    还可以作为列表的元素进行使用
    可以作为返回值返回
    可以作为参数进行传递

    # def fn():
    #     print("我叫fn")
    
    # fn()
    # print(fn)  # <function fn at 0x0000000001D12E18>
    # fn()
    # gn = fn # 函数名可以进行赋值
    # print(gn)
    # gn()
    
    # fn = 666
    # print(fn) # 666
    
    
    # def func1():
    #     print("朱祁镇")
    #
    # def func2():
    #     print("徐阶")
    #
    # def func3():
    #     print("王阳明")
    #
    # def func4():
    #     print("魏忠贤")
    #
    # lst = [func1, func2, func3, func4] # 函数+() 就是调用.
    # print(lst)
    #
    # # lst[0]()
    # # for el in lst: #  el是列表中的每一项.
    # #     el() # 拿到函数. 执行函数
    #
    #
    # a = 10
    # b = 20
    # c = 30
    # lst = [a, b, c]
    # print(lst)
    
    
    # def wrapper():
    #     def inner():
    #         print("我的天, 还可以扎样写")
    #     print(inner) # <function wrapper.<locals>.inner at 0x00000000028989D8>
    #     inner()
    #     return inner
    #
    # ret = wrapper() # <function wrapper.<locals>.inner at 0x00000000028989D8>
    # print(ret)
    # ret()
    
    # def wrapper():
    #     def inner():
    #         print("哈哈哈")
    #     return inner  # 函数名可以像返回值一样返回
    #
    # ret = wrapper()
    # ret() # 在函数外面访问了函数内部的函数
    # ret()
    # ret()
    
    #
    # def func1():
    #     a = 10
    #     return a
    # print(func1())
    
    # 函数可以作为参数进行传递
    
    def func1():
        print("谢晋")
    
    def func2():
        print('杨士奇')
    
    def func3():
        print('徐渭')
    
    def func4():
        print("柳如是")
    
    # 代理. 装饰器的雏形
    def proxy(a): # a就是变量. 形参
        print("我是代理")
        a()
        print("代理执行完毕")
    
    proxy(func1)
    proxy(func3)
    proxy(func4)
    

      

  • 相关阅读:
    hdu2328 Corporate Identity
    hdu1238 Substrings
    hdu4300 Clairewd’s message
    hdu3336 Count the string
    hdu2597 Simpsons’ Hidden Talents
    poj3080 Blue Jeans
    poj2752 Seek the Name, Seek the Fame
    poj2406 Power Strings
    hust1010 The Minimum Length
    hdu1358 Period
  • 原文地址:https://www.cnblogs.com/YangWenYu-6/p/10098525.html
Copyright © 2011-2022 走看看