faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 0 of 1 people (0%) answered Yes

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.
"""