[Python] 纯文本查看 复制代码 def print_msg(): #1.首先定义一个外层函数print_msg
msg = "I'm closure" #3.变量msg
# printer是嵌套函数
def printer(): #4.嵌套函数,定义了printer函数
print(msg) #7.打印msg的值I'm closure
return printer #5.返回printer的内存地址
# 这里获得的就是一个闭包
closure = print_msg() #2.将print_msg重新赋值给closure,此时的closure并没有值只是存放了一个内存地址
# 输出 I'm closure
closure() #6运行printer函数
|