[Python-Dev] The iterator story
Andrew Koenig
ark@research.att.com
23 Jul 2002 09:58:30 -0400
Ping> - Iterators provide just one method, __next__().
Ping> - The built-in next() calls tp_iternext. For instances,
Ping> tp_iternext calls __next__.
Ping> - Objects wanting to be iterated over provide just one method,
Ping> __iter__(). Some of these are containers, but not all.
Ping> - The built-in iter(foo) calls tp_iter. For instances,
Ping> tp_iter calls __iter__.
Ping> - "for x in y" gets iter(y) and uses it as an iterator.
Ping> - "for x from y" just uses y as the iterator.
+1.
Ping> - We have a nice clean division between containers and iterators.
Ping> - When you see "for x in y" you know that y is a container.
What if y is a file? You already said that files are not containers.
Ping> - When you see "for x from y" you know that y is an iterator.
Ping> - "for x in y" never destroys y.
Ping> - "if x in y" never destroys y.
What if y is a file?
Ping> Other notes:
Ping> - The file problem has a consistent solution. Instead of writing
Ping> "for line in file" you write
Ping> for line from file:
Ping> print line
Ping> Being forced to write "from" signals to you that the file is
Ping> eaten up. There is no expectation that "for line from file"
Ping> will work again.
Ah. So you want to break "for line in file:", which works now?
I'm still +1 as long as there is a transition scheme.
Ping> My Not-So-Ideal Protocol
Ping> ------------------------
Ping> All right. So new syntax may be hard to swallow. An alternative
Ping> is to introduce an adapter that turns an iterator into something
Ping> that "for" will accept -- that is, the opposite of iter().
Ping> - The built-in seq(it) returns x such that iter(x) yields it.
Ping> Then instead of writing
Ping> for x from it:
Ping> you would write
Ping> for x in seq(it):
Ping> and the rest would be the same. The use of "seq" here is what
Ping> would flag the fact that "it" will be destroyed.
I prefer "for x from it:
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark