A replacement to closures in python?

Noam Aigerman noama at answers.com
Fri Jan 30 19:36:32 CET 2009


Hi,

I want to create an array of functions, each doing the same thing with a
change to the parameters it uses... something like:

arr=['john','terry','graham']

funcs=[]

for name in arr:

                def func():

                                print 'hello, my name is '+name

                funcs.append(func)

for f in funcs:

                f()

 

And I would like that to print

hello, my name is john

hello, my name is terry

hello, my name is graham

of course... 

Now I understand why the above code doesn't work as I want it to, but is
there some simple workaround for it? 

Thanks, Noam 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090130/6543ffa5/attachment.html>


More information about the Python-list mailing list