[Patches] New builtin function repeat?
Guido van Rossum
guido@python.org
Thu, 06 Apr 2000 09:26:42 -0400
> I made this module some time about but I still think it is neat.
> It creates a new function called repeat (I'm not stuck on the
> name). You can use it like this:
>
> for line in repeat(sys.stdin.readline):
> ...
>
> The repeat function creates a new sequence object. The function
> supplied is called for __getitem__. By default, a false value
> returned from the function will raise an IndexError. You can
> specifiy an alternate termination value. For example:
>
> for line in repeat(sys.stdin.readline, ''):
> ...
>
> would work as well.
>
> While I'm throwing out crazy ideas, how about adding some
> sequence methods to PyFileObject? You could then do:
>
> file = open(...)
> for line in file:
> ...
>
> Too weird? Here is a patch and the module.
All Python 1.7 or Py3K material... To be discussed on python-dev.
I expect to redesign the way for loops interact with sequences; there
will be a separate protocol, probably a method to extract a generator
from an arbitrary object, where the generator has first/next methods.
Then "for line in list" will create a list generator which keeps track
of the loop index, while "for line in file" will create a file
generator which leaves the state up to the file object. Some b/w
compat issues arise.
--Guido van Rossum (home page: http://www.python.org/~guido/)