[Tutor] Calling a function
Ewald Ertl
ewald.ertl at hartter.com
Thu Jun 9 09:19:57 CEST 2005
Hi!
I don't know if I'm right here, because I've tested a simple model of what you're
trying to do:
on Wed, 8 Jun 2005 23:45:59 -0700 Kevin Reeder <reederk at comcast.net> wrote :
---------------------------------------------------------------------------------------------
Kevin Reeder > import time, makezeros
Kevin Reeder >
Kevin Reeder > def do_timing(num_times, *funcs):
Here funcs is a tuple containing all remaining arguments of the functions.
Kevin Reeder > totals = {}
Kevin Reeder > for func in funcs: totals[func] = 0.0
Kevin Reeder > for x in range(num_times):
Kevin Reeder > for func in funcs:
Kevin Reeder > starttime = time.time()
Kevin Reeder > apply(func)
Kevin Reeder > stoptime = time.time()
Kevin Reeder > elapsed = stoptime-starttime
Kevin Reeder > totals[func] = totals[func] + elapsed
Kevin Reeder > for func in funcs:
Kevin Reeder > print "Running %s %d times took %.3f seconds" %
Kevin Reeder > (func.__name__, num_times, totals[func])
Kevin Reeder >
Kevin Reeder > do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply))
You call do_timing() with the number of calls and one Argument, the tuple of functions,
so the funcs in do_timing() is a tuple containing your tuple on the call here.
Calling it this way should solve it:
do_timing(100, makezeros.lots_of_appends, makezeros.one_multiply)
Here is a short extract from the Python-Shell of my test:
>>> def myfuncs( *funcs):
... for func in funcs:
... apply( func )
...
>>> def sayHello():
... print "Hello\n"
...
>>> myfuncs( sayHello, sayHello )
Hello
Hello
>>> myfuncs( ( sayHello, sayHello) )
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in myfuncs
TypeError: 'tuple' object is not callable
------------------- end ----------------------
HTH Ewald
More information about the Tutor
mailing list