闭包函数

闭包的定义
内部的函数引用了外部函数的变量

def f1(b):  # 闭包的常用状态
    def f2():
        print(b)
    return f2

ff = f1('bbb')
ff()

def f1():  # 从内部函数返回一个值到全局
    b = 10
    def f2():
        return b
    return f2()

print(f1())

from urllib.request import urlopen
# ret = urlopen('https://www.baidu.com').read()
# print(ret)
def get_url(url):
    def read1():
        ret = urlopen(url).read()
        print(ret)
    return read1
read_func = get_url('https://www.baidu.com')
read_func()

 

posted on 2017-11-02 14:48  杨小天  阅读(91)  评论(0编辑  收藏  举报