[Zope-dev] Re: [Zope-Checkins] SVN: Zope/trunk/lib/python/OFS/Traversable.py using startswith()

Philipp von Weitershausen philipp at weitershausen.de
Tue Feb 28 15:41:24 EST 2006


Lennart Regebro wrote:
>>-                if name[0] == '_':
>>+                if name.startswith('_'):
> 
> Just a question: Is this only a matter of stylistic changes, or is
> there some, like, speedup involved?

In general, using str.startswith and str.endswith is a failsafe because
''.startswith('_') simply returns False, ''[0] == '_' raises a KeyError.
So, if you really know that your string has at least the length you're
trying to test it against, using the index/slice notation is ok. In all
other cases I would prefer str.startswith and str.endswith.

Philipp


More information about the Zope-Dev mailing list