On Oct 22, 4:41 am, "Francesco Guerrieri" <[EMAIL PROTECTED]> wrote: > On 10/22/07, james_027 <[EMAIL PROTECTED]> wrote: > > > hi, > > > i have a function that I could like to call, but to make it more > > dynamic I am constructing a string first that could equivalent to the > > name of the function I wish to call. how could I do that? the string > > could might include name of the module. > > > for example > > > a_string = 'datetime.' + 'today()' > > > how could I call a_string as function? > > you could use getattr: > > function_name = 'time' # this is a string > module_name = 'time' # this is a string, too > > my_function = getattr(module_name, function_name) # this is the > function object, > # equivalent to my_function = time.time
Not quite. ============================================ >>> function_name = 'time' # this is a string >>> module_name = 'time' # this is a string, too >>> my_function = getattr(module_name, function_name) Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> my_function = getattr(module_name, function_name) AttributeError: 'str' object has no attribute 'time' ============================================ It's actually equivalent to: ============================================ >>> "time".time Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> "time".time AttributeError: 'str' object has no attribute 'time' ============================================ -- http://mail.python.org/mailman/listinfo/python-list