python基础-闭包

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27695659/article/details/84862244

闭包:嵌套函数,内部函数调用外部函数的变量

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()

猜你喜欢

转载自blog.csdn.net/qq_27695659/article/details/84862244