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 variable name binding to different object types at runtime, showing dynamic typing
Python

Understanding Python Dynamic Typing at Runtime

Learn how Python's dynamic typing binds names to objects at runtime, why type hints don't enforce types, and how static and runtime checks can manage the tradeoffs.

dynamic typingduck typingtype hintsruntime dispatch
A diagram showing a variable name being hidden by an inner scope in Python, with overlapping scopes and a shadow effect.
Python

Python Variable Shadowing Explained with Examples

Learn how Python variable shadowing works, why it can cause subtle bugs, and how to avoid it with clear naming and scoping discipline.

pythonvariable scopename resolutionLEGB rule
Diagram showing local and nonlocal variable scope in Python nested functions
Python

Python Local vs Nonlocal Variable: Scope Explained

Understand how Python resolves local and nonlocal variables in nested functions, when to use the nonlocal keyword, and how it differs from global.

Pythonvariable scopenonlocalclosures
Illustration contrasting a local variable inside a function with a global variable at module level in Python
Python

Python Local vs Global Variable: Scope Explained

Understand how Python local and global variables differ, how assignment creates local variables, and when to use the global and nonlocal keywords.

PythonVariable ScopeGlobal VariablesLEGB Rule
Illustration of Python nonlocal keyword showing nested function scope and variable binding.
Python

Python nonlocal keyword: scope and closures

Learn how Python's nonlocal keyword works, when to use it in nested functions, and how it differs from global and local scope.

Pythonscopeclosuresnested functions
Editorial thumbnail showing a Python function box with a variable binding arrow pointing outside the function scope to a module-level container.
Python

How the Python Global Keyword Affects Function Scope

Explains how the Python global keyword changes name resolution inside functions, when it is required, and where nonlocal or return values are better alternatives.

global keywordpython scopenonlocalmodule-level variables
Illustration of a nested function in Python modifying an outer variable using the nonlocal keyword, shown as a scope diagram.
Python

Python Nonlocal Variable: Scope and Usage

Understand the Python nonlocal variable keyword, its role in nested functions, and practical examples for closures.

nonlocalclosuresnested functionsvariable scope
Python code editor showing a global variable being accessed from inside a function with the global keyword highlighted.
Python

Python Global Variables: Scope, Syntax, and Pitfalls

python global variable: Learn how Python global variables work: the global keyword, LEGB scope resolution, mutable object traps, thread safety, and when to avoid them.

python scopeglobal keywordvariable scopingmodule-level state
Diagram showing local variable scope inside a function, with global scope outside.
Python

Python Local Variable Scope: How It Works

Understand how Python local variables work: scope resolution, lifetime, closures, and common errors like UnboundLocalError.

pythonvariable scopeLEGB ruleclosures
Illustration of Python variable scope showing nested scopes and name resolution order.
Python

Python Variable Scope: LEGB, global, and nonlocal

Learn how Python resolves names with the LEGB rule and how to use global and nonlocal to modify variables in module-level and enclosing scopes.

Pythonvariable scopeLEGB ruleclosures