On Fri, Jan 30, 2009 at 10:36 AM, Noam Aigerman <no...@answers.com> wrote:

Hi,

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

I'm not really sure what you're trying to accomplish so there may be a better answer, but in this case if you just pass the name as a keyword arg to the function you're defining it'd work. That way you're saving the current state of 'name' when defining the function, instead of having it look it up at runtime.

I.e.

arr = ['john', 'terry', 'graham']
funcs = []
for name in arr:
    def func(name=name):
        print 'Hi', name
    funcs.append(func)

for f in funcs:
    f()

--Stephen

Attachment: signature.asc
Description: OpenPGP digital signature

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to