美文网首页
Python装饰器3-返回函数

Python装饰器3-返回函数

作者: dnsir | 来源:发表于2019-06-15 11:13 被阅读0次

    函数返回值是函数

    """
    return func in func
    """
    # 可调用对象
    def hi(name = "yasoob"):
        def greet():
            return "now you are in the greet function"
        
        def welcome():
            return "now you are in the welcome function"
    
        if name == "yasoob":
            return greet
        else:
            return welcome
    #a指向的是greet函数(的内存地址)
    a = hi()
    print(a)
    # a函数调用本质是调用greet函数
    print(a())
    
    # b指向的是hi函数(的内存地址)
    b = hi
    # b(name='bidu')返回值是welcome函数地址
    print(b(name="bidu"))
    # b(name='bidu')()是一次函数调用,welcome函数发生调用
    print(b(name = "bidu")())
    

    小结

    Python装饰器本质也是返回一个函数地址,然后运行时发生函数调用

    相关文章

      网友评论

          本文标题:Python装饰器3-返回函数

          本文链接:https://www.haomeiwen.com/subject/aczvfctx.html