python 闭包

例子:

#例1
def func():
    a = 1
    def func1():
        print(a)
    return func1
var = func()
var()

#例2
def func(num):
    def func1(num1):
        print(num+num1)
    return func1
var = func(10)
var(20)

#例3
list = [1,2,3,4,5]
def func(obj):
    def func1():
        obj[0] += 1
        print(obj)
    return func1
var = func(list)
var()
var()
var()

return func1    返回内部函数的引用

posted @ 2018-03-26 16:51  sneike  阅读(103)  评论(0编辑  收藏  举报