Skip to content

Functions In Depth

In Python, functions are values — not special syntax, not second-class constructs. A function can be assigned to a variable, stored in a list, passed as an argument, or returned from another function. This property is called first-class status, and it is the foundation of every advanced pattern in this module.

def greet(name: str) -> str:
return f"Hello, {name}!"
# Assign to a variable
say_hi = greet
# Pass as an argument
def apply(fn, value):
return fn(value)
result = apply(greet, "Python")
print(result) # Hello, Python!

Because greet is just an object, say_hi and greet point to the same function. Calling say_hi("World") is identical to calling greet("World").

LessonCore concept
Args & KwargsPositional, keyword, variadic, and keyword-only parameters
ClosuresNested functions, free variables, and nonlocal
DecoratorsFunctions that wrap other functions; functools.wraps
Generatorsyield, lazy evaluation, and StopIteration

Each lesson is self-contained and builds on this foundation: functions are objects you can manipulate freely.

The snippet below demonstrates two core ideas simultaneously: passing a function as an argument, and returning a function as a value.

def make_multiplier(factor: int):
def multiply(x: int) -> int:
return x * factor
return multiply
double = make_multiplier(2)
triple = make_multiplier(3)
numbers = [1, 2, 3, 4, 5]
doubled = list(map(double, numbers))
tripled = list(map(triple, numbers))
print("doubled:", doubled)
print("tripled:", tripled)
# Functions are objects — we can store them in a list
ops = [double, triple, make_multiplier(10)]
for op in ops:
print(op(7))
What does 'first-class function' mean in Python?
Which built-in function accepts a function and an iterable as arguments?
After writing `f = greet`, which statement is true?