Fastest way to calculate leading whitespace

Mark Dickinson dickinsm at gmail.com
Sun May 9 09:34:38 EDT 2010


On May 9, 6:13 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sat, 08 May 2010 13:46:59 -0700, Mark Dickinson wrote:
> >> However, s[:-len(t)] should be both faster and correct.
>
> > Unless len(t) == 0, surely?
>
> Doh! The hazards of insufficient testing. Thanks for catching that.

I have a love-hate relationship with the negative index semantics for
exactly this reason: code like 'x[-n]' always seems smelly to me.
It's often not what the code author actually wanted, except when n is
guaranteed strictly positive for some reason.  'x[-1]' is fine, of
course.

--
Mark



More information about the Python-list mailing list