摘要: 最近看到stackoverflow上对python闭包的讨论,很有意思,在此记录一下。原文可以去http://stackoverflow.com/questions/233673/lexical-closures-in-python看问题起源是有个程序员提了这么个问题;flist =[]for i in xrange(3): def func(x): return x * i flist.append(func)for f in flist: print f(2)本来预期的结果是生成三个函数,每个函数闭合了变量i,i分别为0 1 2,所以最后打印的结果应该是0 2 4我试了一下这段... 阅读全文
posted @ 2013-08-23 12:30 yametech 阅读(358) 评论(0) 推荐(0) 编辑