[Python-ideas] str.find() and friends support a lists of inputs
Andrew Barnert
abarnert at yahoo.com
Thu Apr 17 22:14:23 CEST 2014
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.
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