[ create a new paste ] login | about

Link: http://codepad.org/fXfIj364    [ raw code | output | fork | 2 comments ]

RogerPate - Python, pasted on Feb 19:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def list_files():
  for fn in ["example.txt", "data.txt"]:
    yield fn, lambda: "sample contents of %s" % fn

for fn, body in list(list_files()):
  if fn.endswith('.txt'):
    print body()

print "---"

# 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()


Output:
1
2
3
4
5
sample contents of data.txt
sample contents of data.txt
---
sample contents of example.txt
sample contents of data.txt


Create a new paste based on this one


Comments:
posted by RogerPate on Feb 19
reply
posted by RogerPate on Feb 19
reply