python第九期学习笔记(闭包)

闭包:㠌套函数,而且内部函数必需要调用外部函数的变量

def outer():
a=1
def inner():
print(a)
print(inner.__closure__)
outer()

闭包的常规用法:
def outer():
a=1
def inner():
print(a)
return inner()
inn=outer()
inn()
 

 

案例:

from urllib.request import urlopen
def get_url():
url="http://www.baidu.com"
def inner():
content=urlopen(url).read()
print(content)
return inner
inn=get_url()
inn()

 

猜你喜欢

转载自www.cnblogs.com/gaoyuxia/p/11745321.html