[Python-checkins] Using string methods in stdlib

Neal Norwitz neal@metaslash.com
2002年6月26日 19:05:03 -0400


(moved to python-dev and changed title)
Fredrik Lundh wrote:
>> > ping@users.sourceforge.net wrote:
> > >
> > > Update of /cvsroot/python/python/dist/src/Lib
> > >
> > > Modified Files:
> > > cgitb.py
> >
> > > + if name[:1] == '_': continue
> >
> > Any reason not to use:
> >
> > if name.startswith('_'): continue
> >
> > ?
>> tried benchmarking?

I wasn't asking because of speed. I don't know
which version is faster and I couldn't care less.
I think using the method is clearer.
> and figuring out that "_" is exactly one character long isn't
> that hard, really.

I agree that for a single character either way is clear.
> (can we please cut this python newspeak enforcement crap
> now, btw. even if slicing hadn't been much faster, there's
> nothing wrong with using an idiom that has worked perfectly
> fine for the last decade...)

I thought the stdlib used startswith/endswith. But I did 
a simple grep just to find where startswith could be used and 
was surprised to find about 150 cases. Many are 1 char,
but there are many others of 5+ chars which make it harder
to determine immediately if the code is correct.
I also see several cases of code like this in mimify:
	line[:len(prefix)] == prefix
and other places where the length is calculated elsewhere, 
(rlcompleter) making it even harder to verify correctness.
Part of the reason to prefer the methods is for defensive programming.
There is duplicate information by using slicing (str & length) and 
it's possible to change half the information and not the other, 
leading to bugs. That's not possible with the methods.
I don't think the stdlib should use every new feature. But I
do think it should reflect the best programming practices and
should be programmed defensively in order to try to avoid future bugs.
Neal

AltStyle によって変換されたページ (->オリジナル) /