简单示例代码二

一、闭包

def outer():
    x = 1
    def inner():
        print(x)
    return  inner #不加括号,返回函数体

f = outer()
f() #调用

二、函数装饰器

def decorate(func):
    def f():
        print("原函数开始了")
        func()
        print("原函数结束了")
    return  f #返回函数体

@decorate  #语法糖
def hello():
    print("我是原函数")

# dec = decorate(hello) #装饰hello函数
# dec() #调用内部函数

hello()
posted @ 2022-02-21 16:06  曾某某scau  阅读(17)  评论(0编辑  收藏  举报