# [Tutor] What Eval() Hath Men Wrought

Magnus Lyckå magnus at thinkware.se
Mon Jun 28 08:57:03 EDT 2004

```At 17:17 2004-06-24 +0100, Alan Gauld wrote:
>You can use the Python apply() function to do a similar thing,
>it takes a callable object and a tuple of arguments to be passed
>to the callable.

There is no longer any need to use apply, since you have
the *args and **kwargs constructs since Python 2.0. See
http://www.amk.ca/python/2.0/index.html section 9.1.

>>> import time
>>> def callme(f, *args, **kwargs):
print "Function:", f, "called with"
print "positional arguments", args, "and"
print "named arguments", kwargs
start = time.clock()
result = f(*args, **kwargs)
stop = time.clock()
print "It took %f seconds" % (stop-start)
return result

>>> import math
>>> callme(math.sin, 0.7)
Function: <built-in function sin> called with
positional arguments (0.69999999999999996,) and
named arguments {}
It took 0.000017 seconds
0.64421768723769102
>>> callme(dir)
Function: <built-in function dir> called with
positional arguments () and
named arguments {}
It took 0.000029 seconds
['args', 'f', 'kwargs', 'start']
>>> def x(a1, a2, p1=1, p2=1):
return a1*p1 + a2*p2

>>> callme(x,3,4,p2=0.5)
Function: <function x at 0x0098CF30> called with
positional arguments (3, 4) and
named arguments {'p2': 0.5}
It took 0.000033 seconds
5.0

--
Magnus Lycka (It's really Lyck&aring;), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language

```