Functions as values
Functions as values means treating a function like any other value: you can store it in a variable, put it in a dictionary and pass it to another function.
Last updated: 30 Sep, 2026 · Python 3.14
Every function in Functions was called straight away. Agent frameworks also store functions and call them later, which starts here.
Storing a function in a variable
def shout(text):
return text.upper()
handler = shout
print(handler("refund please"))
print(shout)REFUND PLEASE <function shout at 0x10a0e1d20>
shout without brackets is the function itself; with brackets, it is a call. handler = shout stores the function, so calling handler runs shout. The last line prints the function, not its result.
Sorting by a field with key=
tickets = [
{"id": 1, "priority": 2},
{"id": 2, "priority": 5},
{"id": 3, "priority": 3},
]
def by_priority(ticket):
return ticket["priority"]
urgent_first = sorted(tickets, key=by_priority, reverse=True)
print([ticket["id"] for ticket in urgent_first])[2, 3, 1]
sorted does not know how to order dictionaries. You pass it a function with key=, and it calls that function on each ticket and orders by what comes back. reverse=True puts the largest first.
def; both make the same kind of function. And addition = lambda a, b: a + b stores a function in a variable, as the first section of this page did with handler. Python's style guide, PEP 8, prefers def for a function you give a name, and a lambda written in place, as in sorted(key=...).def addition(a, b):
return a + b
print(addition(4, 5))
addition = lambda a, b: a + b
print(addition(12, 14))
even1 = lambda a: a % 2 == 0
print(even1(12))9 26 True
The inputs go before the colon and the single expression after it, and the expression's value is returned without return. On this page the lambda goes straight into sorted:
Writing a function in place with lambda
urgent_first = sorted(tickets, key=lambda ticket: ticket["priority"], reverse=True)
print([ticket["id"] for ticket in urgent_first])[2, 3, 1]
lambda ticket: ticket["priority"] is the same function as by_priority, written in place. It can hold one expression and no more, which is why it suits a key=.
Calling a function by its name
A model that uses tools does not run them. It answers with the name of a tool and its arguments, and the program looks the function up and calls it.
def lookup_order(order_id):
return f"Order {order_id} shipped on 3 March."
def refund(order_id):
return f"Refund started for order {order_id}."
tools = {"lookup_order": lookup_order, "refund": refund}
chosen = "refund"
print(tools[chosen]("A17"))Refund started for order A17.
The dictionary maps names to functions. tools[chosen] is a function, and ("A17") calls it. Change chosen and a different function runs, with no if anywhere.
def vs lambda
def | lambda | |
|---|---|---|
| Has a name | Yes | Only if you store it |
| Body | Any number of lines | One expression |
| Typical use | Anything reused | A short key= for sorted |
Where functions as values show up in AI code
- A dictionary of tools, looked up by the name a model asks for.
- Callbacks: a function you hand to a library, which calls it when something happens.
key=by_priority() with brackets calls the function once, with no ticket, and fails. Pass the function itself: key=by_priority.Related
- Previous: Function arguments
- Next: import and modules
- Reference: Lambda Expressions in the Python tutorial
- Set
chosen = "lookup_order". - Set
chosen = "cancel"and read the error. Then check first withif chosen in tools:, and printunknown toolotherwise. - Sort the tickets so the lowest priority comes first.
This is what real progress feels like.