On 3/23/2020 12:02 PM, Rhodri James wrote:
On 23/03/2020 14:50, Dan Stromberg wrote:

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.

Agreed, and I think we shouldn't raise. If raising is important, the user can write a trivial wrapper that raises if no substitution was done. Let's not over-complicate this.

Eric
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/NYJMGSLPW4KFOYUT6ZWL6PSKXCF3EDHM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to