Function generators Python -
i trying implement function generator used n times. idea create generator object, assign object variable , call re-assigned variable function, example:
def generator: [...] yield ... x in xrange(10): function = generator print function(50)
when call print function, observe function(50)
not called. instead output is: <generator object...>
. trying use function 10 times assigning generator function, , using new variable generator function.
how can correct this?
generator functions return generator objects. need list(gen)
convert them list
or iterate on them.
>>> def testfunc(num): in range(10, num): yield >>> testfunc(15) <generator object testfunc @ 0x02a18170> >>> list(testfunc(15)) [10, 11, 12, 13, 14] >>> elem in testfunc(15): print elem 10 11 12 13 14
this question explains more it: the python yield keyword explained
Comments
Post a Comment