Making a python iterator go backwards?

Posted by uberjumper on Stack Overflow See other posts from Stack Overflow or by uberjumper
Published on 2010-05-05T22:23:07Z Indexed on 2010/05/05 22:28 UTC
Read the original article Hit count: 326

Filed under:
|
|

Is there anyway to make a python list iterator to go backwards?

Basically i have this

class IterTest(object):
    def __init__(self, data):
        self.data = data
        self.__iter = None

    def all(self):
        self.__iter = iter(self.data)
        for each in self.__iter:
            mtd = getattr(self, type(each).__name__)
            mtd(each)

    def str(self, item):
        print item

        next = self.__iter.next()
        while isinstance(next, int):
            print next
            next = self.__iter.next()

    def int(self, item):
        print "Crap i skipped C"

if __name__ == '__main__':
    test = IterTest(['a', 1, 2,3,'c', 17])
    test.all()

Running this code results in the output:

a
1
2
3
Crap i skipped C

I know why it gives me the output, however is there a way i can step backwards in the str() method, by one step?

© Stack Overflow or respective owner

Related posts about python

Related posts about iter