The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →A string does not become a function in Python: look up the function object the string names, then call it. For a fixed set of commands, use a dictionary that maps approved strings to functions—the Python FAQ calls this the best approach. Use getattr when resolving an attribute on a known object or module, and do not use eval or exec to dispatch untrusted input.
Use a dictionary for a fixed set of commands
A dispatch dictionary is an explicit allowlist: each accepted command points to the function that should handle it. The string can be a simple external command such as "go", even if the implementation function is named start. This is the approach recommended by the Python Software Foundation’s Programming FAQ.
As an Amazon Associate I earn from qualifying purchases.
def start():
return "started"
def stop():
return "stopped"
actions = {"go": start, "halt": stop}
user_command = "go"
result = actions[user_command]()
The dictionary stores function objects, not function-name strings. Indexing it resolves the command to a callable; the final parentheses invoke that callable. If input may contain an unsupported command, handle the missing key explicitly:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →def add_user():
...
def delete_user():
...
commands = {"add": add_user, "delete": delete_user}
try:
function = commands[input_value]
except KeyError:
raise ValueError(f"Unknown command: {input_value!r}")
result = function()
Use getattr for an attribute on a known object
If the requested function is a method or attribute of an object, class, or module you already have, getattr can resolve it by name. Supplying a default, such as None, avoids an AttributeError when the attribute is absent. Check that the result is callable before invoking it:
#1 Best Overall
method = getattr(service, method_name, None)
if not callable(method):
raise ValueError("Unknown method")
result = method()
When a name comes from an external caller, checking that it is callable is not authorization: an object may expose callable attributes that the caller should not be allowed to invoke. Restrict permitted names with an allowlist or use a dispatch dictionary whose entries represent the approved operations.
Import a module separately when its name is dynamic
If a string identifies a module, load that module with importlib.import_module, then resolve the function attribute. Importing a module and looking up a function on it are separate steps:
Rank #2
import importlib
module = importlib.import_module("my_package.handlers")
handler = getattr(module, "handle_request")
result = handler()
Only import modules your application expects and trusts. If module or function names are provided externally, validate them against an allowlist appropriate to the application.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWhy not use locals(), globals(), eval(), or exec()?
locals() and globals() are scope lookups
These functions expose namespaces, but they are not general-purpose dispatch registries. Which names are present depends on scope, and the behavior of local namespaces has version-specific details; Python’s documentation notes changed locals() semantics in Python 3.13. When the accepted command set is known, a dedicated dictionary makes the permitted operations clearer and avoids relying on incidental names in a scope.
eval() and exec() execute code
eval evaluates a Python expression; exec executes a suite of statements. Neither is needed to look up a function that already exists. The Python 3.14.8 built-in functions reference warns that calling eval with untrusted user-supplied input leads to security vulnerabilities, and gives a corresponding warning for exec. Restricting __builtins__ is not a security mechanism.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




