Language
Functions
Functions are values. Pass them, return them, store them and compose them.
def
7
Default arguments
Hello, world! Hi, world!
Keyword arguments
123 123 123
Variadic parameters
A function without *args rejects extra positional arguments with TypeError. The two variadic forms collect what the declared parameters do not take.
*argscollects extra positional arguments into a tuple.**kwargscollects 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.
api:80 secure=False api:443 secure=True rejected
Argument unpacking at the call site
6 6 6 6
lambda
A lambda is an anonymous function. Its body is a single expression.
42 7 Hi, world
Functions as values
A function can sit in a list or a dict, travel as an argument and come back as a result.
[3, '-0x3', '-3']
A dict of functions replaces a switch statement.
7 12 4
A higher-order function takes or returns a function.
25 10 8 13
Closures
An inner function captures the variables of its enclosing scope by reference.
1 2 3
Capture is by reference. Every lambda made 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 namerebinds the variable of the nearest enclosing function. Thecounterclosure above relies on it.global namerebinds the module-level variable.
7
Reading an outer variable needs no declaration. Only rebinding does.
15
Recursion
3628800
True False
A pure function called again with the same immutable positional arguments can return a cached result without running its body. Design lists when the cache applies.
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
yield from delegates to another generator or any iterable.
[0, 1, 2, 10, 20]
yield from is also an expression. It evaluates to the return value of the subgenerator. A generator can pass a result back to the one that delegates to it.
returned done [1, 2]
Generators are one-way producers. gen.send(value), gen.throw(exc) and gen.close() are not exposed. For flow in both directions, use the cooperative scheduler and pass values through arguments and return values.
Generator expressions
A generator expression is a generator written inline. It computes each value when asked and can be read once.
30 0 [1, 4] []
Decorators
A decorator wraps another callable. It applies to functions and to 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
Function attributes
Functions carry writable attributes, like any object. Decorators use them for metadata.
getattr,hasattr,setattranddelattrwork on them.- An assigned
__name__wins over the declared one.
10 n/a True