Can I make my class play nice with the Python '"in" keyword?
<p>I'm heading into hour 5 or so of my experience with Python, and so far I've been pretty impressed with what it can do. My current endeavor is to make a short attempt at a Stream class, the code for which follows:</p>

<pre><code>class Stream:

    """A Basic class implementing the stream abstraction. """

    def __init__(self,data,funct):
        self.current = data
        self._f = funct

    def stream_first(self):
        """Returns the first element of the stream"""
        return self.current

    def stream_pop(self):
        """Removes and returns the first element of the stream. """
        temp = self.current
        self.current = self._f(self.current)
        return temp
</code></pre>

<p>Enjoying modest success there, I tried to make a BoundedStream class which behaves basically like the unbounded one, except that at a certain point it runs out of elements. My question now is, seeing that any such Bounded Stream has some finite number of elements, it should be possible to iterate over them. If I were using an explicit list, I could use Python's <code>in</code> keyword and a <code>for</code> loop to do this cleanly. I would like to preserve that cleanliness for my own class. Is there some method I could implement, or any other language feature, that would allow me to do this? Any answers or other help you might provide to a rookie would be greatly appreciated!</p>

<p>-David</p>

<p>P.S.</p>

<p>For those who want to know, the impetus for a Bounded Stream is that I tried the builtin <code>range</code> function, but Python claimed that the range I wanted to look at was too large. I have turned to Streams in the interest of memory efficiency.</p>
