Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

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

Example
def shout(text):
    return text.upper()

handler = shout
print(handler("refund please"))
print(shout)

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=

Example
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])

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.

Writing a lambda function · from Tutorial 13- Python Lambda Functions · 0:42 to 4:12
Two corrections to the video. A lambda is not faster than a function written with 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=...).
ExampleFrom the video, run here
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))

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

Example
urgent_first = sorted(tickets, key=lambda ticket: ticket["priority"], reverse=True)
print([ticket["id"] for ticket in urgent_first])

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.

Example
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"))

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

deflambda
Has a nameYesOnly if you store it
BodyAny number of linesOne expression
Typical useAnything reusedA 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.
Watch out. key=by_priority() with brackets calls the function once, with no ticket, and fails. Pass the function itself: key=by_priority.
Try it yourself
  • Set chosen = "lookup_order".
  • Set chosen = "cancel" and read the error. Then check first with if chosen in tools:, and print unknown tool otherwise.
  • Sort the tickets so the lowest priority comes first.

This is what real progress feels like.