The built-in function eval will do what you want. All the usual warnings about executing arbitrary user-supplied code apply.
If there are a finite number of predefined functions, you should avoid eval
and use a lookup table instead (i.e. Dict
). Never trust your users.