Skip to main content

How to Call a Function by Its Name in Python with a Dictionary

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
ACTIONS = {"<name>": <function>, "<other name>": <other function>}

function = ACTIONS.get(<name>)
if function is not None:
function(<arguments>)
PartWhat 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​

call_by_name.py
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"))

The call_by_name.py script in a macOS-style code window: greet and cheer functions, an ACTIONS dictionary that maps their names to them, and a loop over greet, cheer and dance that looks each one up with get and calls it or reports that it doesn't exist.

  • ACTIONS maps the text "greet" to the function greet, and "cheer" to cheer.
  • ACTIONS.get(action) finds the function for each name, or None for a name that isn't in the dictionary.
  • function("Sam") calls whichever function was found.

Step 2: Run It​

Terminal
uv run python call_by_name.py
Output
Hello, Sam!
Well done, Sam!
There's no action called dance.

The output in a terminal: Hello, Sam!, Well done, Sam!, and 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​

MistakeWhat happensDo this instead
Looking a name up with ACTIONS["dance"]KeyError: 'dance' for a name that isn't thereUse 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 callableStore the function itself, without ()

Troubleshooting​

SymptomLikely causeFix
TypeError: '...' object is not callableThe dictionary holds a function's result instead of the functionRemove 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​