Le dim. 22 mars 2020 à 01:45, Dennis Sweeney
<[email protected]> a écrit :
> For accepting multiple prefixes, I can't tell if there's a consensus about
> whether
> ``s = s.cutprefix("a", "b", "c")`` should be the same as
>
> for prefix in ["a", "b", "c"]:
> s = s.cutprefix(prefix)
>
> or
>
> for prefix in ["a", "b", "c"]:
> if s.startwith(prefix):
> s = s.cutprefix(prefix)
> break
>
> The latter seems to be harder for users to implement through other means, and
> it's the
> behavior that test_concurrent_futures.py has implemented now, so maybe that's
> what we
> want.
I expect that "FooBar".cutprefix(("Foo", "Bar")) returns "Bar". IMO
it's consistent with "FooFoo".cutprefix("Foo") which only returns
"Foo" and not "":
https://www.python.org/dev/peps/pep-0616/#remove-multiple-copies-of-a-prefix
If you want to remove both prefixes,
"FooBar".cutprefix("Foo").cutprefix("Bar") should be called to get "".
> Also, it seems more elegant to me to accept variadic arguments, rather than a
> single
> tuple of arguments. Is it worth it to match the related-but-not-the-same API
> of
> "startswith" if it makes for uglier Python? My gut reaction is to prefer the
> varargs, but
> maybe someone has a different perspective.
I suggest to accept a tuple of strings:
str.cutprefix(("prefix1", "prefix2"))
To be consistent with startswith():
str.startswith(("prefix1", "prefix2"))
cutprefix() and startswith() can be used together and so I would
prefer to have the same API:
prefixes = ("context: ", "ctx:")
has_prefix = False
if line.startswith(prefixes):
line = line.cutprefix(prefixes)
has_prefix = True
A different API would look more surprising, no? Compare it to:
prefixes = ("context: ", "ctx:")
has_prefix = False
if line.startswith(prefixes):
line = line.cutprefix(*prefixes) # <== HERE
has_prefix = True
The difference is even more visible is you pass directly the prefixes:
.cutprefix("context: ", "ctx:")
vs
.cutprefix(("context: ", "ctx:"))
Victor
--
Night gathers, and now my watch begins. It shall not end until my death.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/JTFKF2ASUR5QV3I73O72RHYL5S72OGDW/
Code of Conduct: http://python.org/psf/codeofconduct/