This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author apalala
Recipients apalala, ezio.melotti, mrabarnett, rhettinger, serhiy.storchaka, terry.reedy
Date 2020-01-17.13:41:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1579268511.14.0.413355873821.issue39165@roundup.psfhosted.org>
In-reply-to
Content
The analysis done by Terry bypasses the fact that `search(...)` returns `None` when there is no match, so indexing or calling methods in its result is not safe code. 

`findall()` returns an empty list when there is no match.

`findalliter()` returns an empty iterator when there is no match.

`findfirst()` may return a `default` value when there is no match.

If `search()` is proposed to replace `findall()[0]`, then the idiom has to be (depending on the case):

m[0] if (m := re.search(...)) else '0'
m.groups() if (m := re.search(...)) else '0'

In contrast, `findfirst()` returns a value that is the same as `findall()` when there is a match, or a `default` if there is no match.

m[0] if (m := re.findall(...)) else '0'

Compare with:

findfirst(..., default='0')
History
Date User Action Args
2020-01-17 13:41:51apalalasetrecipients: + apalala, rhettinger, terry.reedy, ezio.melotti, mrabarnett, serhiy.storchaka
2020-01-17 13:41:51apalalasetmessageid: <1579268511.14.0.413355873821.issue39165@roundup.psfhosted.org>
2020-01-17 13:41:51apalalalinkissue39165 messages
2020-01-17 13:41:50apalalacreate