Trouble with generator

Denis S. Otkidach ods at strana.ru
Sat Jan 4 08:48:57 EST 2003


After first item your generator implicitly returns, that is turned in
StopIteration.  Put yield statement in loop:


    def _linkGenerator(self, baseUrl):

        maxPages = 4

        for counter in range(maxPages):

            if baseUrl[0:7] == 'http://':
                url = baseUrl[7:]
            else:
                url = baseUrl


            yield string.join(('http://www.google.com/search?q=link:',
                               url,
                               '&hl=es&lr=&ie=UTF-8&filter=0',
                               '&start=',
                               str((counter - 1) * 10)), '')


On 4 Jan 2003, Boethius wrote:

B> I'm getting a StopIteration exception with this generator:
[...]
B> When I call the next() method for the second time, I get this
B> error:
B>
B> >>> gen = g._linkGenerator( 'http://www.easyjob.net')
B> >>> gen.next()
B> 'http://www.google.com/search?q=link:www.easyjob.net&hl=es&lr=
B> &ie=UTF-8&filter=0&start=0'
B> >>> gen.next()
B> Traceback (most recent call last):
B>   File "<stdin>", line 1, in ?
B> StopIteration

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]






More information about the Python-list mailing list