Python: defining functions on the fly

2019-03-16 19:16发布

I have the following code:

 funcs = []
 for i in range(10):
   def func():
      print i
   funcs.append(func)

 for f in funcs:
   f()

The problem is that func is being overriden. Ie the output of the code is:

9
9
9
...

How would you solve this without defining new functions?

The optimal solution would be to change the name of the function. Ie:

for i in range(10):
   def func+i():
...

(or some other weird syntax)

标签: python lambda
3条回答
Rolldiameter
2楼-- · 2019-03-16 19:50

The problem is not that func is being overwritten, it's that the value of i is being evaluated when the function is called, not when it is defined. If you want to evaluate i at definition time, put it in the function declaration, as a default argument to func.

funcs = []
for i in range(10):
    def func(value=i):
        print value
    funcs.append(func)

for f in funcs:
    f()

Default arguments are evaluated once, when the function is defined, so the incrementing loop will not affect them. This would work just as well if you used

def func(i=i):
    print i

but I used the name value to make it clear which name is being used within the function.

查看更多
可以哭但决不认输i
3楼-- · 2019-03-16 20:02

Returning func from another function is safest.

查看更多
做个烂人
4楼-- · 2019-03-16 20:07

You could try

for i in range(10):
    def func(j=i):
        print j
    funcs.append(func)
for f in funcs:
    f()
查看更多
登录 后发表回答