如何理解闭包的概念
上面这篇文章写的特别好,重点大概如下
几个概念
- code block:作为一个单元(Unit)被执行的一段python程序文本。例如一个模块、函数体和类的定义等。
- local variable:如果name在一个block中被绑定,该变量便是该block的一个local variable。
- global variable:如果name在一个module中被绑定,该变量便称为一个global variable。
- free variable: 如果一个name在一个block中被引用,但没有在该代码块中被定义,那么便称为该变量为一个free variable。
几个案例
# If a name is bound in a block, it is a local variable of that block.
# If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.)
# If a variable is used in a code block but not defined there, it is a free variable.
# 案例一
# +=是赋值操作, 在一个code block中如果有赋值语句,那变量就是本block中的, 不回去父block中去寻找
def outer_func():
loc_var = "local variable"
def inner_func():
loc_var += " in inner func"
return loc_var
return inner_func
clo_func = outer_func()
clo_func()
# 案例二,
# The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.
# 但是没有说执不执行, 也就是说只要有赋值语句, 在当前的block中就是可见的, 但是如果不真正执行的话, 是不会执行绑定操作的, 也就是只有赋值语句, 没有绑定操作
def get_select_desc(name, flag, is_format = True):
if flag:
sel_res = 'Do select name = %s' % name
return sel_res if is_format else name
print get_select_desc('Error', True, True)
# 案例三
def outer_func(out_flag):
if out_flag:
loc_var1 = 'local variable with flag'
else:
loc_var2 = 'local variable without flag'
def inner_func(in_flag):
return loc_var1 if in_flag else loc_var2
return inner_func
clo_func = outer_func(True)
print clo_func(False)
闭包的几个易错点
# 闭包可能引起的问题:
# 1. 循环中去循环变量,会被后一个覆盖
# 返回闭包列表fs之前for循环的变量的值已经发生改变了,而且这个改变会影响到所有引用它的内部定义的函数。
# 因为在函数my_func返回前其内部定义的函数并不是闭包函数,只是一个内部定义的函数。
def my_func():
fs = []
for i in xrange(3):
def func():
return i * i
fs.append(func)
return fs
fs1, fs2, fs3 = my_func()
print fs1()
print fs2()
print fs3()
# 类似这种写法, lambda经常中招, lambda _x=x: _x
def my_func(*args):
fs = []
j = 0
for i in xrange(3):
def func():
return j * j
fs.append(func)
j = 2
return fs
# 2. 修饰类时, 类已经变成了修饰器内的那个func
def counter(cls):
obj_list = []
def wrapper(*args, **kwargs):
new_obj = cls(*args, **kwargs)
obj_list.append(new_obj)
print "class:%s'object number is %d" % (cls.__name__, len(obj_list))
return new_obj
return wrapper
@counter
class my_cls(object):
STATIC_MEM = 'This is a static member of my_cls'
def __init__(self, *args, **kwargs):
print self, args, kwargs
print self.__class__.__name__
print my_cls.STATIC_MEM
c = my_cls()



