A curious bit of code...

Zachary Ware zachary.ware+pylist at gmail.com
Thu Feb 13 22:20:09 CET 2014


On Thu, Feb 13, 2014 at 3:01 PM, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2014-02-13, Zachary Ware <zachary.ware+pylist at gmail.com> wrote:
>> C:\tmp>py -m timeit -s "key = ''" "key[0] == '<' and key[-1] == '>'"
>> Traceback (most recent call last):
>>   File "P:\Python34\lib\timeit.py", line 292, in main
>>     x = t.timeit(number)
>>   File "P:\Python34\lib\timeit.py", line 178, in timeit
>>     timing = self.inner(it, self.timer)
>>   File "<timeit-src>", line 6, in inner
>>     key[0] == '<' and key[-1] == '>'
>> IndexError: string index out of range
>
> The corrected version
>
> key and key[0] == '<' and key[-1] == '>'
>
> probably still wins the Pretty Unimportant Olympics.

Indeed, see the last set of timings and moral ;)

-- 
Zach



More information about the Python-list mailing list