4

I am trying to understand Iterability in Python.

As I understand, __iter__() should return an object that has next() method defined which must return a value or raise StopIteration exception. Thus I wrote this class which satisfies both these conditions.

But it doesn't seem to work. What is wrong?

class Iterator:
    def __init__(self):
        self.i = 1

    def __iter__(self):
        return self

    def next(self):
        if self.i < 5:
            return self.i
        else:
            raise StopIteration

if __name__ == __main__:
    ai = Iterator()
    b  = [i for i in ai]
    print b
4
  • You'll need "main" in quotes Commented Mar 16, 2009 at 7:28
  • there's a typo. It should be self.i += 1 rather than i += 1 Commented Mar 16, 2009 at 7:34
  • your current code seems to work, is there still a problem? Commented Mar 16, 2009 at 7:36
  • Your current version has a space inside 'main', which is incorrect. Commented Mar 16, 2009 at 7:47

5 Answers 5

9

Your Iterator class is correct. You just have a typo in this statement:

if __name__ ==' __main__':

There's a leading whitespace in the ' __main__' string. That's why your code is not executed at all.

Sign up to request clarification or add additional context in comments.

Comments

4

i will never become greater than 5 if you don't increment it in next()

Comments

4

I think in most cases it might be enough to write a generator function that uses yield instead of writing a full-fledged iterator.

Comments

1

Your current code seems to work. Instead i'll show you some more iterators/generators.

the simplest builtin with exactly your behavior.

Iterator2 = xrange(2,5)

A direct translation of your class to a generator

def Iterator3():
    i = 1
    while i < 5:
        i += 1
        yield i

a generator composed from generators in the python standard library

import itertools
Iterator4 = itertools.takewhile( 
                        lambda y : y < 5, 
                        itertools.count(2) )

a simple generator expression (not very exciting...)

Iterator5 = ( x for x in [2, 3, 4] )

Comments

0

Your code has two problems:

  • if name == '__main__': (missing quotes)
  • def next . . .: you don't increment i anywhere

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.