I tried searching for an answer to this in the google groups archives
but I failed to come up with anything. So I hope no one minds me asking
this, though I'm sure it's been asked a million times before.
I was hoping that the following code would print "Hello, world!", one
character per line. But instead it prints an exclamation mark for each
character of the string. I'm sure it's no coincidence that the last
value of c is '!', but beyond that I don't understand what's happening.
# --- begin code ---
def callback(arg):
print arg
funcs = []
for c in 'Hello, world!':
funcs.append(lambda: callback(c))
for f in funcs:
f()
# --- end code ---
What I'm trying to do is create a 0-args callback by binding an
argument to a 1-arg function. But failing, miserably :)
I'm using Python 2.3.5 as installed by default on Mac OS X 10.4 if it
matters.
Thanks,
Edd