codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
# fixed version: def list_files(): for fn in ["example.txt", "data.txt"]: # "captures" fn for each lambda yield fn, lambda fn=fn: "sample contents of %s" % fn for fn, body in list(list_files()): if fn.endswith('.txt'): print body.func_code.co_varnames # the fn hack is "exposed" on the above line, or with help(body) print body() print "---" # another way, which creates a new closure for each # and doesn't "expose" the hack (by using another hack, of course :P): def list_files(): for fn in ["example.txt", "data.txt"]: # "captures" fn for each lambda yield fn, (lambda fn=fn: lambda: "sample contents of %s" % fn)() for fn, body in list(list_files()): if fn.endswith('.txt'): print body.func_code.co_varnames # can also use help(body) to see the difference print body()
Private
[
?
]
Run code
Submit