python 函数 闭包 (节省内存空间)

#闭包:嵌套函数,内部函数调用外部函数的变量
# def outer():
#     a = 1
#     def inner():
#         print(a)
#     inner()
# outer()

def outer():
    a = 1
    def inner():
        print(a)
    return inner
inn = outer()
inn()

# import urllib  #模块
from urllib.request import urlopen
# ret = urlopen('http://www.xiaohua100.cn/index.html').read()
# print(ret)
# def get_url():
#     url = 'http://www.xiaohua100.cn/index.html'
#     ret = urlopen(url).read()
#     print(ret)
#
# get_url()

def get_url():
    url = 'http://www.xiaohua100.cn/index.html'
    def get():
        ret = urlopen(url).read()# 获取网页的源码
        print(ret)
    return get#返回函数

get_func = get_url()#函数接收
get_func()#输出 调用

猜你喜欢

转载自www.cnblogs.com/xiao-zhi/p/9877759.html
今日推荐