[Solved] Do python support generated objects? [closed]


Some languages, you showed a python example, support generators that don’t have a return statement, they yield a value on each iteration.

In Python, the statement:

result = someFunc()

makes result refer to a generator object, which you can then use the next() builtin to get the next value.

In the Python for loop the next() is called for you, so we iterate through a lazy list of values (a lazy list is where only one item at a time is provided).

So actually your statement:

result = someFunc()
for x in result:
    print x

will produce the same! That is because result is a generator object: the name result is a bad choice.

2

solved Do python support generated objects? [closed]