On 03Apr2019 14:54, Steven D'Aprano <steve@pearwood.info> wrote:
Now imagine it's five years from now, and you're using Python 3.11, and you came across code somebody (possibly even you!) wrote:
ifname = ifname.cutsuffix(':')
Would you say "Damn, I wish that method had never been added!" and replace it with the earlier code above?
Just a late followup to this thread. The other month I found myself doing the endwith/s=s[:-n] shuffle yet again, and wrote a pair of cutprefix and cutsuffix functions. They're available in my "cs.lex" PyPI module if anyone wants to use them. Their signature is: prefix = cutsuffix(original_string, suffix) if prefix is original_string: # suffix not present ... else: # suffix present, proceed using prefix and the converse for cutprefix. Cheers, Cameron Simpson <cs@cskk.id.au>