1

I want to have an iterator, which essentially returns each element of a list attribute. The code below works, but requires an ugly instance variable causing problems:

As soon as I create another iterator instance (instead of re-using testIter) the nested loop works fine, but this does not seem an option for my full-life problem.

How can I get rid of the instance variable without sacrificing the neat for something in syntax?

class MyIterator(object):
    def __init__(self, myList):
        self._list = myList

    def __iter__(self):
        self.cursor = 0
        return self

    def next(self):
        try:
            nextElem = self._list[self.cursor]
        except IndexError:
            raise StopIteration
        else:
            self.cursor += 1
            return nextElem


def test(l):
    testIter = MyIterator(l)
    for line in testIter:
        for column in testIter:
            print "{}/{}".format(line, column),
        print

if __name__ == "__main__":
    test("abc")
guidot
  • 5,095
  • 2
  • 25
  • 37

2 Answers2

0

why not make the method static using decorator @staticmethod

@staticmethod
def test(l):
    testIter = MyIterator(l)
    for line in testIter:
        for column in testIter:
            print "{}/{}".format(line, column),
        print

For Reference

with this use can directly call the function with the class name.

MyIterator.test("abc")
Community
  • 1
  • 1
saikumarm
  • 1,565
  • 1
  • 15
  • 30
  • Thank you for the hint, but I don't think the demo routine should be part of the class. The answer does not really address the core question, unfortunately. – guidot Sep 04 '17 at 19:50
0

This is, how I found it to work. I don't remember, where I found the proposal, that __iter__ should only return self and do not much else, but it is much neater that way.

class MyIterator(object):
    def __init__(self, myList):
        self._list = myList

    def __iter__(self):
        for nextElem in self._list:
            yield nextElem

def test(l):
    testIter = MyIterator(l)
    for line in testIter:
        for column in testIter:
            print("{}/{}".format(line, column))
        print()

if __name__ == "__main__":
    test("abc")
guidot
  • 5,095
  • 2
  • 25
  • 37