Python 101Free
FUNCTIONS

Higher Order Functions

Treating functions as values you can pass around.

SECTION 01

Functions as values

A function in Python is just an object. You can assign it to a variable, store it in a list or dict, pass it to another function, or return it from one. There is nothing special about its name or its position in the source.

This is what people mean when they say functions are "first-class". The language treats them like any other value, and any pattern that works for ordinary values works for functions too.

Once you internalize this, a lot of patterns become obvious. Callbacks, event handlers, sorting keys, decorators, plugin systems: they all rely on passing functions around. The core feature is just "functions are values".

python
def double(x):
    return x * 2

fns = [double, abs, str]
fns[0](5)        # 10

def apply(fn, x):
    return fn(x)
apply(double, 7) # 14
1 more section

Create a free account to access all sections and animations.

Create free accountAlready have an account? Sign in