pushback iterator

Mike Kazantsev mk.fraggod at gmail.com
Sun May 17 18:15:52 CEST 2009


Somehow, I got the message off the list.

On Sun, 17 May 2009 17:42:43 +0200
Matus <matusu at gmail.com> wrote:

> > Sounds to me more like an iterator with a cache - you can't really pull
> > the line from a real iterable like generator function and then just push
> > it back.
> 
> true, that is why you have to implement this iterator wrapper

I fail to see much point of such a dumb cache, in most cases you
shouldn't iterate again and again thru the same sequence, so what's
good hardcoding (and thus, encouraging) such thing will do?

Besides, this wrapper breaks iteration order, since it's cache is LIFO
instead of FIFO, which should rather be implemented with deque instead
of list.

> > If this "iterator" is really a list then you can use it as such w/o
> > unnecessary in-out operations.
> 
> of course, it is not a list. you can wrap 'real' iterator using this
> wrapper (), and voila, you can use pushback method to 'push back' item
> received by next method. by calling next again, you will get pushed back
> item again, that is actually the point.

Wrapper differs from "list(iterator)" in only one thing: it might not
make it to the end of iterable, but if "pushing back" is common
operation, there's a good chance you'll make it to the end of the
iterator during execution, dragging whole thing along as a burden each
time.

> > And if you're "pushing back" the data for later use you might just as
> > well push it to dict with the right indexing, so the next "pop" won't
> > have to roam thru all the values again but instantly get the right one
> > from the cache, or just get on with that iterable until it depletes.
> > 
> > What real-world scenario am I missing here?
> > 
> 
> ok, I admit that that the file was not good example. better example
> would be just any iterator you use in your code.

Somehow I've always managed to avoid such re-iteration scenarios, but
of course, it could be just my luck ;)

-- 
Mike Kazantsev // fraggod.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090517/0e1ecc74/attachment.sig>


More information about the Python-list mailing list