Linked Questions

Popular Questions

How to test this function in Python... TypeError: object not callable

Asked by At

Can't test the function slowest_call with 3 arguments.

This is one of my learning exercises. First I defined the time_call function, then I'm trying to use it as an input in another function, slowest_call.

Maybe my inputs are not quite right (what can I put as the 4th input?!). Not sure what else to try.

from time import sleep
from time import time
t = time()
sleep(duration)


 """Return the amount of time the given function takes (in seconds) when called with the given argument """

def time_call(fn, arg):
    t0 = time()
    fn(arg)
    t1 = time()
    elapsed = t1 - t0
    return elapsed
time_call(sleep, 4)  # testing it, it works

 """"Return the amount of time taken by the slowest of the following function
    calls: fn(arg1), fn(arg2), fn(arg3) """

def slowest_call(fn, arg1, arg2, arg3):
return max(time_call(fn, arg1), time_call(fn, arg2), time_call(fn, arg3))

I'm testing it like this

slowest_call(time_call(sleep,5), time_call(sleep,3), time_call(sleep,2))

TypeError: slowest_call() missing 1 required positional argument: 'arg3'

It gives gives me the error while it should generate the longest time as an answer (5...).

Related Questions