闭包的作用:
-
- 可以保护你的变量不收侵害,闭包对外界不开放(与global可以随时改动全局变量做对比)(python 设计模式的开放封闭原则)
-
- 可以让一个变量常驻内存(eg:爬虫中由于网络延迟的原因.可以让url常驻内存)
def outer():
a = 10 # 常驻内存, 为了inner执行的时候有值.
def inner()
闭包的作用:
def outer():
a = 10 # 常驻内存, 为了inner执行的时候有值.
def inner()