
On 23/03/2020 14:50, Dan Stromberg wrote:
On Fri, Mar 20, 2020 at 3:28 PM Victor Stinner <vstinner@python.org> wrote:
The builtin ``str`` class will gain two new methods with roughly the following behavior::
def cutprefix(self: str, pre: str, /) -> str: if self.startswith(pre): return self[len(pre):] return self[:]
I tend to be mistrustful of code that tries to guess the best thing to do, when something expected isn't found.
How about:
def cutprefix(self: str, pre: str, raise_on_no_match: bool=False, /) -> str: if self.startswith(pre): return self[len(pre):] if raise_on_no_match: raise ValueError('prefix not found') return self[:]
I'm firmly of the opinion that the functions should either raise or not, and should definitely not have a parameter to switch behaviours. Probably it should do nothing; if the programmer needs to know that the prefix wasn't there, cutprefix() probably wasn't the right thing to use anyway. -- Rhodri James *-* Kynesim Ltd