What's the Pythonic way to do this?

Doug Rosser da_rosser at yahoo.com
Fri Sep 10 16:35:46 EDT 2004


class Cycle(object):

    def __init__(self, inputList):
        object.__init__(self)
        self.index = 0
        self.limit = len(inputList)
        self.list = inputList

    def next(self):
        """
        returns the next element of self.list, jumping
        back to the head of the list if needed.
        Yes, this is an infinite loop. (Use with caution)
        Arguments:
            none
        Returns:
            the next element of self.list
        """
        if self.index+1 < self.limit:
            self.index+=1
            return self.list[self.index-1]
        else:
            self.index=0
            return self.list[self.limit-1]



More information about the Python-list mailing list