Message360186
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') |
|
Date |
User |
Action |
Args |
2020-01-17 13:41:51 | apalala | set | recipients:
+ apalala, rhettinger, terry.reedy, ezio.melotti, mrabarnett, serhiy.storchaka |
2020-01-17 13:41:51 | apalala | set | messageid: <1579268511.14.0.413355873821.issue39165@roundup.psfhosted.org> |
2020-01-17 13:41:51 | apalala | link | issue39165 messages |
2020-01-17 13:41:50 | apalala | create | |
|