Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Illustration of a Python function returning another function, representing closures and factories.
Python

Returning Functions in Python: Closures and Factories

Learn how to return functions in Python, how closures capture state, when to use factory functions, and how to avoid common closure pitfalls.

closuresdecoratorsfunction factorieshigher-order functions
A visual metaphor of a Python function being passed as an argument to another function, representing higher-order functions.
Python

Python Function as Argument: Patterns and Pitfalls

Learn how to pass a Python function as an argument, including callbacks, higher-order functions, lambdas, and common mistakes.

higher-order functionscallbackslambdafunction arguments
Illustration of Python functions being passed as arguments and returned from functions, showing first-class function behavior.
Python

Python First-Class Functions in Practice

Learn how Python treats functions as first-class objects: pass them, return them, store them, and use them in higher-order functions and decorators.

higher-order functionsclosuresdecoratorsfunctional programming
Diagram showing a nested function referencing a variable from an enclosing function via the nonlocal keyword.
Python

Python nonlocal in Closures: Scope Rules

Understand how Python's nonlocal keyword works in closures: when assignment must rebind an enclosing variable, how it differs from global and local scopes, and how to use it to manage state.

closuresvariable scopenested functionsnonlocal keyword
Diagram showing a Python closure capturing a variable from an outer function, with arrows indicating the reference to the variable cell.
Python

Python Closure Variable Capture Explained

See how Python closures capture variables, why late binding returns the final loop value, and how to control captured values with default arguments and nonlocal.

closuresvariable scopenonlocallate binding
Illustration of a Python closure capturing a variable from an outer function's scope.
Python

Python Closures: How Nested Functions Capture State

Learn how Python closures capture variables from enclosing scopes, how nonlocal modifies captured state, and when a closure is the right tool compared with classes and lambdas.

closuresnested functionsnonlocalfunction factories
Diagram illustrating Python function scope with nested functions and variable lookup order.
Python

Python Function Scope: Local, Global, and Nonlocal

Understand how Python function scope resolves variable names inside functions with the LEGB rule, and learn when to use global, nonlocal, and closures.

pythonscopeclosuresLEGB
Diagram of an outer function containing a nested inner function with a closure arrow showing captured state
Python

Python Inner Function: Syntax, Scope, and Use Cases

Understand python inner function syntax, closure behavior, and practical uses like decorators and factory functions.

nested functionsclosuresdecoratorsnonlocal
Editorial illustration of concentric nested shapes representing a Python nested function and its enclosing scope.
Python

Python Nested Function: Scope, Closures, and Use Cases

Understand how nested function scope works in Python, how closures capture variables, and when inner functions improve code structure.

closuresscopingpython functionsdecorators
Illustration of a Python function call stack approaching a limit line, with a warning sign for recursion depth.
Python

Python Recursion Limit: Setting and Handling Deep Recursion

python recursion limit: Understand Python's recursion limit, how to check and adjust it, and why iterative alternatives are often safer for deep recursion.

recursionsys.setrecursionlimitRecursionErroriterative algorithms