[Python-ideas] oneof() and multi split and replace for stirngs
Josiah Carlson
jcarlson at uci.edu
Tue Jan 23 18:32:02 CET 2007
"Calvin Spealman" <ironfroggy at gmail.com> wrote:
>
> On 1/23/07, Josiah Carlson <jcarlson at uci.edu> wrote:
> > Whether it is a tuple being passed, or a "magic" container, I don't
> > think it matters; though I would lean towards a tuple because it is 5
> > less characters to type out, and one fewer data types to worry about.
>
> I had talked to others and the concensus was against the tuples for
> ugliness. s.split((a,b), c) wasn't a popular choice, but
> s.split(oneof(a, b), c) reads better.
oneof = tuple
Now it reads better. Stick with tuple.
> > This has been discussed before in python-dev, I believe the general
> > consensus was that it would be convenient at times, but I also believe
> > the general consensus was "use re";
>
> It seems like we have a history of useful string operations being
> moved away from "use re" to "dont use re", such that the slightly
> recent startswith and endswith methods, and even split and replace
> themselves. I would like to see less reasons for people to worry with
> regular expressions until they actually need them. If we can provide a
> better way to get the job done, that seems like a great idea.
Well, startswith and endswith is still computable without re or the
methods, and turns out to be faster (for certain startswith and endswith
operations) to do...
if st[:X] == sub:
or
if st[-X:] == sub:
(more specifically, when sub is a constant and X is previously-known)
> The oneof type isn't just a single use thing. Usecode may often make
> use of it, and other types could benefit such as doing a lookup with
> d[oneof(1,2,3)] (where order would matter for priority). I think this
> semantic collection type would be very useful in a number of contexts
> where we would currently just loop or duplicate code.
I took the time to look for a thread discussing something like this
feature before. And I found it. Turns out I was wrong:
http://mail.python.org/pipermail/python-dev/2005-September/056119.html
That thread discusses an enhancement to str.[r|l|]strip(), where one
could specify a list of strings to be trimmed from the end(s), not only
just a group of characters (does the equivalent of .startswith and
.endswith).
What was the outcome? Use the X-line function that can be defined as Y.
I'll stick with "use re".
- Josiah
More information about the Python-ideas
mailing list