How to Call a Function by Its Name in Python with a Dictionary
Sometimes the function to run is only known as text: a command someone typed, an action in a file, a tool name sent by another program. A dictionary that maps each name to its function turns that text into a call, with no long if/elif chain. This guide runs two actions by name and handles one that doesn't exist, with real output from Python 3.15.2.
Syntax
ACTIONS = {"<name>": <function>, "<other name>": <other function>}
function = ACTIONS.get(<name>)
if function is not None:
function(<arguments>)
| Part | What it means |
|---|---|
{"<name>": <function>} | Each name points at a function. The function is written without (), so it's stored, not called |
ACTIONS.get(<name>) | The function for that name, or None if the name isn't there |
function(<arguments>) | Calls the function you found |
This works because a function is a value like any other. Python's tutorial: "A function definition associates the function name with the function object in the current symbol table." And: "Other names can also point to that same function object and can also be used to access the function".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
def greet(name: str) -> str:
return f"Hello, {name}!"
def cheer(name: str) -> str:
return f"Well done, {name}!"
ACTIONS = {"greet": greet, "cheer": cheer}
for action in ["greet", "cheer", "dance"]:
function = ACTIONS.get(action)
if function is None:
print(f"There's no action called {action}.")
else:
print(function("Sam"))

ACTIONSmaps the text"greet"to the functiongreet, and"cheer"tocheer.ACTIONS.get(action)finds the function for each name, orNonefor a name that isn't in the dictionary.function("Sam")calls whichever function was found.
Step 2: Run It
uv run python call_by_name.py
Hello, Sam!
Well done, Sam!
There's no action called dance.

"greet" and "cheer" each ran their own function. "dance" wasn't in the dictionary, so .get() returned None and the script said so instead of crashing. Adding an action means adding one entry to ACTIONS.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Looking a name up with ACTIONS["dance"] | KeyError: 'dance' for a name that isn't there | Use ACTIONS.get(name) and check for None |
Storing a call, {"greet": greet("x")} | The dictionary holds the text greet returned, and calling it gives TypeError: 'str' object is not callable | Store the function itself, without () |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: '...' object is not callable | The dictionary holds a function's result instead of the function | Remove the () from the dictionary's values |
FAQ
Can the functions take different arguments?
They can, but then the caller has to know which arguments each one needs. Keeping every function in the dictionary to the same arguments, like name here, keeps the call simple.
Why not use eval() or globals() to find the function?
Those would run whatever name the text holds, including functions you never meant to expose. The dictionary allows exactly the names you put in it.
Conclusion
The ACTIONS dictionary turned the names "greet" and "cheer" into calls of their functions, and .get() caught "dance" as a name with no function.
Additional Resources
- Defining Functions — functions as objects that names point to.
- dict.get — looking a key up with a default.
