On 24Mar2019 18:39, MRAB <pyt...@mrabarnett.plus.com> wrote:
On 2019-03-24 08:42, Alex Grigoryev wrote:
Following the discussion here <https://link.getmailspring.com/link/7d84d131-65b6-4ef7-9c43-51957f9df...@getmailspring.com/0?redirect=https%3A%2F%2Fbugs.python.org%2Fissue36410&recipient=cHl0aG9uLWlkZWFzQHB5dGhvbi5vcmc%3D>
This has a subtle bug:
In [2]: def rtrim(s, seq):
...: return s[:-len(seq)] if s.endswith(seq) else s
...:
If len(seq) == 0, then rtrim will return ''.
It needs to be:
def rtrim(s, seq):
return s[ : len(s) - len(seq)] if s.endswith(seq) else s
Or:
return s[:-len(seq)] if seq and s.endswith(seq) else s
which I think more readable.
For the record, like others, I suspect I've written ltrim/rtrim code
many times.
I'm +0.9 on the idea: it feels like a very common operation and as shown
above rtrim at least is fairly easily miscoded. (I think most of my own
situations were with strings I know are not empty, often literals, but
that doesn't really detract.)
Like others I'm against the name 'trim" itself because of PHP's homonym
which means what "strip" means in Python (and therefore doesn't mean
what "trim" is proposed to mean here). "clip"?
I'm +0.9 rather than +1 entirely because the operation feels so...
trivial, which usually trips the "not everything needs a method"
argument. But it is also very common.
Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/