Python_闭包_27

#闭包:嵌套函数,内部函数 并且必须调用外部函数的变量
def outer():
    a = 1
    def inner():
        print(a)
    inner()
    print(inner.__closure__) # 说明是一个闭包函数
outer()

def outer():
    a = 1
    def inner():
        print(a)
        print('haha')
    return inner #
inn = outer()
inn()     # 在函数的外部 直接使用函数内部的函数

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


# import urllib  #模块
from urllib.request import urlopen
ret = urlopen('https://www.baidu.com/').read()
print(ret)

def get_url():
    url = 'https://www.baidu.com/'
    ret = urlopen(url).read()
    print(ret)

get_url()



def get_url():
    url = 'https://www.baidu.com/'
    def get():
        ret = urlopen(url).read()
        print(ret)
    return get

get_func = get_url()
get_func()
原文地址:https://www.cnblogs.com/LXL616/p/10660699.html