[issue16304] re: Match Objects always have a boolean value of True

Steven D'Aprano report at bugs.python.org
Wed Oct 24 00:23:26 CEST 2012


Steven D'Aprano added the comment:

I don't think the relevant information -- that re match and search return None in the case of no match -- is that distant from the paragraph quoted. This fact is already mentioned FOUR times in the page, in each of the re.match, re.search functions and regex.match, regex.search methods, plus implied by the helper function used in the examples.

Personally I think no change is required, but if the docs are going to be changed, I recommend going all the way and showing the usual re idiom with an example:

Match Objects always have a boolean value of ``True``.  Since ``None`` (which has a boolean value of ``False``) is returned when there is no match, you can test whether there was a match with a simple ``if`` statement. For example::

mo = re.search(pattern, string)
if mo:
    process(mo)

----------
nosy: +stevenjd

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16304>
_______________________________________


More information about the Python-bugs-list mailing list