词汇闭包是如何工作的?
flist = []for i in xrange(3): def func(x): return x * i flist.append(func)for f in flist: print f(2)
lambda
my @flist = ();foreach my $i (0 .. 2){ push(@flist, sub {$i * $_[0]});}foreach my $f (@flist){ print $f->(2), "\n";}
i
flist = []def outer(): for i in xrange(3): def inner(x): return x * i flist.append(inner)outer()#~ print i # commented because it causes an errorfor f in flist: print f(2)
i
米脂
慕勒3428872
相关分类