Entry
Lazy sequences (was: Readlines(N))
Jul 5th, 2000 10:01
Nathan Wallace, Hans Nowak, Snippet 212, Evan Simpson
"""
Packages: oop;basic_datatypes.lists
"""
"""
> What about generalizing on the idea of 'xrange' (lazy sequences), making
> it a standard class
Your wish is my command:
"""
class Lazy:
def __init__(self, n):
self._n = n
def __getitem__(self, i):
if i < self._n:
return i
else:
raise IndexError, "end of Lazy sequence"
"""
Of course, this doesn't do the full xrange thing, but I hope you get the
idea. A lazy sequence can be nothing more than an object with a
__getitem__ method.
>, and re-implementing 'xrange' and 'readlines' with
>it ?
Why would you want to re-implement either of these? They're both already
as lazy as is practical, and more efficient than any class could be.
"""