[Python-ideas] str.find() and friends support a lists of inputs
MRAB
python at mrabarnett.plus.com
Fri Apr 18 01:16:28 CEST 2014
On 2014-04-17 21:14, Andrew Barnert wrote:
> On Apr 17, 2014, at 11:52, Alex Rodrigues <lemiant at hotmail.com>
> wrote:
>
>> It's a fairly common problem to want to .find() or .replace() or
>> .split() any one of multiple characters.
>
> I like your solution, except for one thing. Explicitly requiring a
> list of arguments rather than, say, a tuple or an iterator, seems
> unnecessarily restrictive. However, allowing any iterable of strings
> doesn't work because a string is itself an iterable of strings.
>
> There are a few cases where Python deals with this problem by
> treating tuples specially (e.g., % formatting), but I don't think
> anyone wants to extend that solution.
>
str.startswith and str.endswith accept either a string or a tuple of
strings, but not a list of strings nor any other iterable, so does it
matter if str.find, etc, accepted a tuple but not a list?
> You could almost handle these problems with *args (replace replaces
> any of args[:-1] with args[-1]), except that all of them have
> optional parameters at the end.
>
> You could have a keyword-only argument to specify an iterable of
> strings (in which case you can't use any positional arguments), but
> that's a pretty weird interface.
>
> Or you could just add some new methods: split_any, replace_any, etc.
> But str already has a lot of methods; do we really want more?
>
> I'd love to see an answer that works here, because I agree that it
> would make a lot of code simpler, and especially code that novices
> want to write.
More information about the Python-ideas
mailing list