Functions
Functions are values. Pass them, return them, store them, compose them.
def
7
Default arguments
Hello, world! Hi, world!
Keyword arguments
123 123 123
Variadic parameters
*args collects extra positional arguments into a tuple. **kwargs collects extra keyword arguments into a dict.
6 60
[('host', 'api'), ('port', 443)]Keyword-only parameters
A bare * marks the following parameters as keyword-only. They must be passed by name. A positional argument that would reach them raises TypeError, as does any positional argument beyond the declared parameters when there is no *args.
api:80 secure=False api:443 secure=True rejected
Argument unpacking at the call site
6 6 6 6
lambda
An anonymous function. The body is a single expression.
42 7 Hi, world
First-class functions
Store functions in data structures and pass them as arguments.
[3, '-0x3', '-3']
7 12 4
Function attributes
Functions carry writable attributes, like any object. getattr / hasattr / setattr / delattr work on them, and an assigned __name__ wins over the declared one. Decorators use these for metadata.
10 n/a True
Higher-order functions
Functions that take or return functions.
25 10
8 13
Closures
An inner function captures the variables of its enclosing scope by reference.
1 2 3
Because capture is by reference, every lambda in a loop sees the same loop variable. Bind the current value through a default argument.
10 11 12
global and nonlocal
Assignment inside a function creates a local unless declared otherwise. nonlocal name rebinds the nearest enclosing function’s variable. That is the shared cell the counter closure above relies on. global name rebinds the module-level variable.
7
Reading an outer variable needs no declaration. Only rebinding does.
15
Recursion
3628800
True False
Pure functions are memoized after two calls with the same arguments. The VM detects purity statically (no I/O, no mutation, no raise, no yield) and confirms it at runtime. Any call that performs a side effect, including a builtin like
Generators
A function containing yield produces its sequence lazily. Pull values with next() or iterate with for.
0 1 4 9 16
1 2
[1, 2, 3, 4, 5]
yield from
Delegate to another generator or any iterable.
[0, 1, 2, 10, 20]
yield from is also an expression. It evaluates to the subgenerator’s return value, so a generator can pass a result back to its delegating caller.
returned done [1, 2]
Generators are one-way producers.
gen.send(value),gen.throw(exc), andgen.close()are not exposed. For bidirectional flow, use the cooperative scheduler and pass values through arguments and return values.
Generator expressions
A generator inline.
30 5
Decorators
A decorator wraps another callable. It applies to both functions and classes (see Classes).
calling with (3, 4) 7
Stacked decorators apply bottom-up.
12
A parameterized decorator is a factory. The outer function takes the decorator arguments and returns the actual decorator. The wrapped function captures both scopes.
hi world hi world hi world