闭包函数

'''
闭包:内部函数包含对外部作用域而非全局作用域的引用
'''
# def f1():
# x = 1
# def f2():
# print(x)
# return f2
# f=f1()
# print(f)
# f()

# '''
# def 外函名():
# ...
# def 内函名()
# 引用外函
# return 内涵名
# '''

#闭包应用 惰性计算

#下面这个是做爬虫用的
from urllib.request import urlopen
# res=urlopen('http://www.baidu.com').read()
# print(res.decode('utf-8'))

def index(url):
def get():
return url.open(url).read()
return get
res = index('http://www.baidu.com')
#res()
#print(res().decode('utf-8'))
print(res.__closure__[0].cell_contents)
原文地址:https://www.cnblogs.com/lazyball/p/6916635.html