[Python-Dev] str.lstrip bug?

Isaac Schwabacher ischwabacher at wisc.edu
Tue Mar 10 21:32:03 CET 2015


On 15-03-10, Ethan Furman  wrote:
> On 03/10/2015 10:57 AM, Isaac Schwabacher wrote:
> > On 15-03-10, Facundo Batista wrote:
> >> On Tue, Mar 10, 2015 at 2:27 PM, lou xiao wrote:
> >>
> >>> tiny➜ ~ python
> >>> Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
> >>> [GCC 4.8.1] on linux2
> >>> Type "help", "copyright", "credits" or "license" for more information.
> >>> >>> a='device_info'
> >>> >>> a.lstrip('device')
> >>> '_info'
> >>> >>> a.lstrip('device_')
> >>> 'nfo'
> >>
> >> On one hand, this is the "development of python itself" list; this
> >> issue was more aimed to the general python list, of you was sure that
> >> this is a real bug, to the issue tracker.
> >>
> >> On the other hand, this is not a bug! If you pass a parameter to
> >> lstrip it will (quoted from its help) "remove characters in chars
> >> instead.", so the moment you pass "device_", it removes all those
> >> characers from the left... note that the 'i' is removed twice.
> > 
> > That said, I bet this is the most common string-munging operations that *isn't* available as a single function in the stdlib. I know my bash code is full of ${filename%.suffix} and such, and the fact that in python I have to either import re or resort to some combination of (starts|ends)with, r?partition, slicing and an if-clause makes that sort of code much more verbose and harder to read. Pathlib's Path.with_suffix helps in some but not all of these cases. Maybe the stdlib should have a simple way to do this? It could even be added as a kwarg (exact=False) to str.[lr]strip to minimize the effect on the API; alternatively it could be str.strip(prefix|suffix).
> 
> Nice way to bring it on-topic!
> 
> And yes, that would be nice.

Yes. When you are converting a code base from bash to python, you *really* don't want python to push back.

> --
> ~Ethan~


More information about the Python-Dev mailing list