[issue39165] Completeness and symmetry in RE, avoid `findall(...)[0]`

Juancarlo Añez report at bugs.python.org
Sat Jan 4 07:46:26 EST 2020


Juancarlo Añez <apalala at gmail.com> added the comment:

There's no way to assert that `findall(...)[0]` is efficient enough in most cases. It is easy to see that that it is risky in every case, as runtime may be exponential, and memory O(len(input)). A mistake in the regular expression may easily result in an out-of-memory, which can only be debugged with a series of tests using `search()`.

A problem with `re.search(...)` is that id doesn't have the return value semantics of `findall(...)[0]`, and those semantics seem to be what appeal to Python programmers. It takes several lines of code (the ones in `findalliter()`) to have the same result as `findal(...)[0]` when using `search()`. `findall()` is the sole, lonely function in `re` with its return-value semantics.

Also this proposal embeds `first()` within the body of `findfirst(...)`, but by the implementation one should consider if `first()` shouldn't be part of `itertools`, perhaps with a different name, like `take_one()`.

One should also consider that although third-party extensions to `itertools` already provide the equivalent of `first()`, `findalliter()` and `findfirst()` do not belong there, and there are no mainstream third-party extensions to `re` where they would fit.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39165>
_______________________________________


More information about the Python-bugs-list mailing list