[Tutor] Problem with Learning Python example

Bob Gailer bgailer at alum.rpi.edu
Thu Aug 11 22:45:39 CEST 2005


At 02:10 PM 8/11/2005, ->Terry<- wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>
>Hello,
>
>I'm working my way through Learning Python 2nd Ed. and
>I've ran across something I don't understand in Chapter
>27, page 482, in Timing section. I checked the errata on
>O'Reilly's web-site and it only mentions a missing comma
>in the print statement in timings.py, which I caught when
>I first ran the program. I've proof-read these 2 files
>several times and even had the wife proof-read it and I'm
>fairly confident I'm not missing any errors. Can someone
>guide me in the right direction?
>
>My system is Slackware 10.1 and Python version is 2.4.1
>
>- --------------------------------
>* makezeros.py
>- --------------------------------
>
>def lots_of_appends():
>      zeros = []
>      for i in range(10000):
>          zeros.append(0)
>
>def one_multiply():
>      zeros = [0] * 10000
>
>
>- --------------------------------
>* timings.py
>- --------------------------------
>
>import time, makezeros
>def do_timing(num_times, *funcs):

*funcs means treat the rest of the arguments passed to the function as a 
tuple. So given a call like:
do_timing(100, func1, func2), funcs = (func1, func2), and for func in funcs 
yields func1, then func2
whereas a call like you are using:
do_timing(100, (func1, func2)), funcs = ((func1, func2)), and for func in 
funcs yields (func1, func2)
so change do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply))
to do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply)

>      totals = {}
>      for func in funcs:
>          totals[func] = 0.0
>          starttime = time.clock()        # record starting time
>          for x in range(num_times):
>              for func in funcs:
>                  apply(func)
>          stoptime = time.clock()          # record ending time
>          elapsed = stoptime - starttime   # difference yields time elapsed
>          totals[func] = totals[func] + elapsed
>      for func in funcs:
>          print "Running %s %d times took %.3f seconds" % (func.__name__, 
> num_times, totals[func])
>
>do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply))
>
>
>- -------------------------------
>* error message
>- -------------------------------
>
>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 9, in do_timing
>      apply(func)
>TypeError: 'tuple' object is not callable
>
>
>Thanks,
>- --
>      Terry
>
>       ,-~~-.___.     Terry Randall <tvbareATsocketDOTnet>
>      / |  '     \
>     <   )        0    Linux Counter Project User# 98233
>      \_/, ,-----'
>         ====          //
>        /  \-'~;    /~~~(0)
>       /  __/~|   /      |   If only Snoopy had Slackware...
>     =( ______| (________|
>
>"He is your friend, your partner, your defender, your dog.
>You are his life, his love, his leader. He will be yours,
>faithful and true, to the last beat of his heart. You owe
>it to him to be worthy of such devotion."    -- Unknown
>
>           (Best viewed with a mono-spaced font.)
>
>-----BEGIN PGP SIGNATURE-----
>Version: GnuPG v1.2.7 (GNU/Linux)
>
>iD8DBQFC+7CwQvSnsfFzkV0RAqVoAJwIodb3VcBEYlneak47zjrIJdsLagCgg0xL
>lss+Go8izUgD9ekCE7B5nKE=
>=fzYk
>-----END PGP SIGNATURE-----
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor

Bob Gailer
mailto:bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 



More information about the Tutor mailing list