[Tutor] More problems with Learning Python example

Alan G alan.gauld at freenet.co.uk
Fri Aug 12 10:48:50 CEST 2005


Something odd here...


> def do_timing(num_times, *funcs):
>     totals = {}
>     for func in funcs:

Here you assign func to each function in turn.

>         totals[func] = 0.0

And here you create a key with it

>         starttime = time.clock()        # record starting time
>         for x in range(num_times):
>             for func in funcs:

And the same here, but the result will be that when you exit this
loop func will always be the last function.

>         totals[func] = totals[func] + elapsed

But you won't have created a key for the last function so
this falls over.

> Traceback (most recent call last):
>   File "timings.py", line 16, in ?
>     do_timing(100, makezeros.lots_of_appends, 
> makezeros.one_multiply)
>   File "timings.py", line 12, in do_timing
>     totals[func] = totals[func] + elapsed
> KeyError: <function one_multiply at 0x403eaf0c>

I'm not quite sure what the second funcs loop is doing, but thats
the reason for the key error.

HTH,

Alan G. 



More information about the Tutor mailing list