Something weird about re.finditer()

Justin Ezequiel justin.mailinglists at gmail.com
Wed Apr 15 05:10:16 EDT 2009


On Apr 15, 4:46 pm, Gilles Ganault <nos... at nospam.com> wrote:
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>
> #Here, get web page and put it into "response"
>
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
>         print "No block found"
> else:
>         print "Before blocks"
>         for block in blocks:
>                 #Never displayed!
>                 print "In blocks"
> ======
>
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?
>
> Thank you for any tip.

because finditer returns a generator which in your case just happens
to be empty

>>> import re
>>> patt = re.compile('foo')
>>> gen = patt.finditer('bar')
>>> gen is None
False
>>> gen == None
False
>>> gen
<callable-iterator object at 0x00E55B70>
>>> list(gen)
[]
>>>




More information about the Python-list mailing list