calling a function from string
Jarek Zgoda
jzgoda at o2.usun.pl
Mon Oct 22 06:46:03 EDT 2007
Trent Nelson napisaĆ(a):
>> 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?
>
> Use 'eval' in one of the following fashions:
>
> a_string_1 = 'datetime.' + 'today'
> a_string_2 = 'datetime.' + 'today()'
>
> eval(a_string_1)()
> eval(a_string_2)
Do not use eval(). Not only it's deprecated, it's also unsafe.
--
Jarek Zgoda
Skype: jzgoda | GTalk: zgoda at jabber.aster.pl | voice: +48228430101
"We read Knuth so you don't have to." (Tim Peters)
More information about the Python-list
mailing list