Something weird about re.finditer()

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Apr 15 05:14:56 EDT 2009


On Wed, 15 Apr 2009 10:46:28 +0200, Gilles Ganault wrote:

> Since "blocks" is no longer set to None after calling finditer()... but
> doesn't contain a single block... what does it contain then?

It probably took you twenty times more time and effort to ask the 
question than it would have to look for yourself.


>>> import re
>>> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>>> x = re_block.finditer("nothing to see here")
>>> x is None
False
>>> x
<callable-iterator object at 0xb7f5ecec>
>>> list(x)
[]




BTW, testing for None with == is not recommended, because one day 
somebody might pass your function some strange object that compares equal 
to None. Although it wouldn't have solved your problem, the recommended 
way to test if an object is None is with the `is` operator.



-- 
Steven



More information about the Python-list mailing list