Get function passed as an argument name inside another function
Question:
I am trying to measure elapse time for different functions with simple method like this:
def taketime(executable,exname='method'):
tic = time.time()
exname = executable.__name__
out = executable
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
Executable is another method, can be whatever. Nevertheless, the program does not work as the line exname = executable.__name__
is actually already running executable and trying to get the property name from the output.
What is the correct way to get the name of the executable passed to another function?
Small test program (not working):
import time
def taketime(executable,exname=None):
tic = time.time()
if exname is None: exname = executable.__name__
out = executable
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
# -----------------------------------------
def hello():
print('Hello!')
return
dd = taketime(hello())
Of course it works when I do this:
dd = taketime(hello(),'hello')
Answers:
You are calling the target function before it is passed into your taketime()
function. Also, you don’t call anything inside your function either. You meant to write your code like this:
import time
def taketime(executable, *args, **kwargs):
exname = executable.__name__
tic = time.time()
out = executable(*args, **kwargs)
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
# -----------------------------------------
def hello():
print('Hello!')
return
dd = taketime(hello)
Note that:
- the call to
taketime
is like this: taketime(hello)
- the target function is actually called like this:
out = executable()
- supply extra arguments like this:
taketime(hello, arg1, arg2)
I am trying to measure elapse time for different functions with simple method like this:
def taketime(executable,exname='method'):
tic = time.time()
exname = executable.__name__
out = executable
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
Executable is another method, can be whatever. Nevertheless, the program does not work as the line exname = executable.__name__
is actually already running executable and trying to get the property name from the output.
What is the correct way to get the name of the executable passed to another function?
Small test program (not working):
import time
def taketime(executable,exname=None):
tic = time.time()
if exname is None: exname = executable.__name__
out = executable
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
# -----------------------------------------
def hello():
print('Hello!')
return
dd = taketime(hello())
Of course it works when I do this:
dd = taketime(hello(),'hello')
You are calling the target function before it is passed into your taketime()
function. Also, you don’t call anything inside your function either. You meant to write your code like this:
import time
def taketime(executable, *args, **kwargs):
exname = executable.__name__
tic = time.time()
out = executable(*args, **kwargs)
toc = time.time()
print('Time taken for ',exname,' ',toc-tic,' sec')
return out
# -----------------------------------------
def hello():
print('Hello!')
return
dd = taketime(hello)
Note that:
- the call to
taketime
is like this:taketime(hello)
- the target function is actually called like this:
out = executable()
- supply extra arguments like this:
taketime(hello, arg1, arg2)