Not sure if this is called a generator, a factory or something else, but it's not doing what I would expect.
d = {}
for i in range(10):
def func():
print i
d[i]=func
print d
for f in d:
d[f]()
This outputs:
9
9
9
9
9
9
9
9
9
9
But I was expecting:
0
1
2
3
4
5
6
7
8
9
I want to store the current state of func at each step of the for loop, but it seems that the dictionary is getting a reference(?) to the function. How do I stop this?
Also, if it is storing a reference to the function, I would epect this to print out 'overridden', but it still prints all 9s. Why is that?
d = {}
for i in range(10):
def func():
print i
d[i]=func
print d
def func():
print 'overridden'
for f in d:
d[f]()
Thanks!