Python: creating a class that could be iterated circularly

1736 views python
2

I want to create, in Python, a class behaving like a list but that could be iterated circularly use case example:

myc = SimpleCircle()
print(len(myc))
j = iter(myc)
for i in range (0, 5):
    print(next(j))

it will print a b c d a

the code I tried so far is the one below I know the issue is with my __next__

method which by the way seems ignored, I can use next even if I don't implement it

class SimpleCircle:
    def __init__(self):
        self._circle = ['a', 'b', 'c', 'd']
        self._l = iter(self._circle)


    def __len__(self):
        return len(self._circle)

    def __iter__(self):
        return (elem for elem in self._circle)

    def __next__(self):
        try:
            elem = next(self._l)
            idx = self._circle.index(elem)
            if idx < len(self._circle):
                return elem
            else:
                return self._circle[0]
        except StopIteration:
            pass

answered question

Are you trying to implement itertools.cycle?

2 Answers

2

This actually already exists with itertools.cycle, for example:

from itertools import cycle

for x in cycle(['a', 'b', 'c', 'd']):
    print(x)

will keep repeating the element.

Next you here mix up the iterable, and the iterator, those are frequently different things.

As an iterable we can keep iterating from self._circle:

class SimpleCircle:
    def __init__(self):
        self._circle = ['a', 'b', 'c', 'd']

    def __len__(self):
        return len(self._circle)

    def __iter__(self):
        if not self._circle:
            raise StopIteration
        while True:
            yield from self._circle

posted this
7

Here's a basic non-itertools implementation:

class Cyclic:
    def __init__(self, data):
        self._data = list(data)

    def __iter__(self):
        while True:
            yield from self._data

cycle = Cyclic(['a', 'b', 'c', 'd'])
for i, x in zip(range(5), cycle):
    print(x)

posted this

Have an answer?

JD

Please login first before posting an answer.