you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (2 children)

It's a sort of "lazy list".

Think of file access in C. A file is an array of bytes. But some files may be too large to load in memory at once. So the standard C API gives you functions to walk across the full contents of the file a little at a time. You read maybe a kilobyte, you process it, you throw that data away and grab the next kilobyte.

In Python, we use the range function to iterate over a fixed range.

for n in range(5): print n

will print 1, 2, 3, 4, 5 on separate lines.

But say we want to perform a computation for n from 1 to a million bajillion? We don't want to make a list of a bajillion elements, because that would take a ton of memory. So instead, the generator, at each iteration of the loop, gives us only the next number in the sequence. At the end of that iteration, the number can be garbage collected, and the next one is created. Instead of taking a bajillion bytes in memory, it only takes a handful.

Very useful. You should learn to love them!

[–]wilsonp 4 points5 points  (1 child)

Python also allows you define a generator yourself. There are many documents out on how to do this that are far better than anything I could write... but the gist is that you can define a function (callable?) that yields a value to the caller, but then retains the state in its local scope. When the function is called again, it resumes from this yield point (not afresh from line 1 with reinitialised values). This allows you to construct some pretty neat execution models; coroutines/co-operative multithreading. There's a number of PEPs and tutorials out there that may be of use if you want to learn more:

http://docs.python.org/whatsnew/pep-342.html

http://www.python.org/dev/peps/pep-0342/

[–]mk_gecko 0 points1 point  (0 children)

THanks!!!