Skip to content

Instantly share code, notes, and snippets.

@sanlee42
Created February 27, 2015 07:35
Show Gist options
  • Save sanlee42/704d35b23d603d5bb794 to your computer and use it in GitHub Desktop.
Save sanlee42/704d35b23d603d5bb794 to your computer and use it in GitHub Desktop.
#! /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)
@sanlee42
Copy link
Author

结果:
<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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment