Calling function from a string

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Dec 8 21:41:34 EST 2008


On Mon, 08 Dec 2008 14:29:01 -0800, Chris Rebert wrote:

> On Mon, Dec 8, 2008 at 2:23 PM, Robert Dailey <rcdailey at gmail.com>
> wrote:
>> Hi,
>>
>> I have a string representing the name of a function in Python 3.0. How
>> can I call the function name represented by this string *without*
>> creating a mapping?
> 
> Assuming the function is within scope:
> 
> return_val = vars()[the_string](arguments, go, here)


Or use eval("%s(arguments go here)" % the_string)

(untested in Python 3, but should work)


Are you aware of the great big enormous security hole you're creating if 
the_string is untrusted data?



-- 
Steven



More information about the Python-list mailing list