Blog by Sumana Harihareswara, Changeset founder
Top, Iterators and Generators, and Git, Emacs, and REPL Tips
Hi, reader. I wrote this in 2013 and it's now more than five years old. So it may be very out of date; the world, and I, have changed a lot since I wrote it! I'm keeping this up for historical archive purposes, but the me of today may 100% disagree with what I said then. I rarely edit posts after publishing them, but if I do, I usually leave a note in italics to mark the edit and the reason. If this post is particularly offensive or breaches someone's privacy, please contact me.
Dumping into a post some things I've learned recently, trying to disregard the potential "you didn't know that already?!?!" surprise, feigned or genuine, that people might impose on me.*
>>> a = [3,6,9] >>> a [3, 6, 9] >>> iter(a) <listiterator object at 0x7f5c8c8da490> >>> s = iter(a) >>> type(s) <type 'listiterator'> >>> s.next() 3 >>> s.next() 6 >>> s.next() 9 >>> s.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> r = "captain" >>> w = iter(r) >>> w.next() 'c' >>> w.next() 'a' >>> w.next() 'p' >>> w.next() 't'
>>> def foo(): ... yield "first" ... yield "second" ... yield "last" ... >>> b = foo() >>> type(b) <type 'generator'> >>> b.next() 'first' >>> b.next() 'second' >>> b.next() 'last' >>> b.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
I read (bits of) so very very many pages about this, and fellow students tried to help me get this (thank you, Joe and Gideon), and then yesterday I paired with Jessica McKellar and she sealed the deal and I think I get it now! As she explained, you might want to use generators if you want to get an infinite series of values (e.g., all the even numbers up to infinity). Or if you're crunching numbers and it takes hella resources to do this particular part of the crunching on THE WHOLE DATASET ALL AT ONCE, with generators you can just crunch one input at a time and yield it up, then move to the next input in the sequence seamlessly when needed. You can speed up bottlenecks in your assembly line by doing particular computations in a just-in-time way.
06 Nov 2013, 13:59 p.m.
C. Scott Ananian
06 Nov 2013, 14:58 p.m.
06 Nov 2013, 22:19 p.m.
06 Nov 2013, 22:21 p.m.
11 Nov 2013, 12:36 p.m.