Created
February 27, 2015 07:35
-
-
Save sanlee42/704d35b23d603d5bb794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/env python | |
def gen_func1(n): | |
for i in range(n): | |
callback = lambda : i | |
yield callback | |
def gen_func2(n): | |
for i in range(n): | |
def genf(i): | |
return lambda: i | |
callback = genf(i) | |
yield callback | |
def test(func): | |
l = [] | |
print func | |
for f in func(2): | |
print "first loop:",f() | |
l.append(f) | |
for f in l: | |
print "second loop:",f() | |
test(gen_func1) | |
test(gen_func2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
结果:
<function gen_func1 at 0x10a2b1578>
first loop: 0
first loop: 1
second loop: 1
second loop: 1
<function gen_func2 at 0x10a2b15f0>
first loop: 0
first loop: 1
second loop: 0
second loop: 1