On Thu, Dec 5, 2019 at 6:16 PM Juancarlo Añez <[email protected]> wrote:
> It’s unfortunate that these functions aren’t better matched. Why is there
>> a simple-semantics find-everything and a match-semantics find-iteratively
>> and find-one? But I don’t think adding a simple-semantics find-one that
>> works by inefficiently finding all is the right solution.
>>
>
> The proposed implementation for *findfirst()* is:
>
> *return next(finditer(pattern, text, flags=flags), default=default)*
>
>
Um, finditer() returns a Match object, and IIUC findfirst() should return a
string, or a tuple of groups if there's more than one group. So the actual
implementation would be a bit more involved. Something like this, to match
findall() better:
for match in re.finditer(pattern, text, flags=flags):
# Only act on first match
groups = match.groups()
if not groups:
return match.group(0) # Whole match
if len(groups) == 1:
return groups[0] # One match
return groups
# No match, use default
return default
Alternatively, replace the first line with this:
match = re.search(pattern, text, flags=flags)
if match is not None:
(There are apparently subtle differences between re.search() and
re.findall() -- not sure if they matter in this case.)
And if the point of proposing first is that novices will figure out how to
>> write first(findall(…)) so we don’t need to add findfirst, then I think we
>> need findfirst even more, because novices shouldn’t learn that bad idea.
>>
>
Yes, my point exactly.
> I posted another thread to argue in favor of *first()*, independently of
> *findfirst().*
>
Also agreed, I've observed that as a common pattern.
--
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/RC2IAT2IP55XFYZLZ2ENAXZCXQCHER2F/
Code of Conduct: http://python.org/psf/codeofconduct/