Understanding Python code blocks and execution units

I was reading about Python’s execution model and found something confusing. According to the official docs, a block in Python refers to code that gets executed as a single unit. The documentation mentions that modules, function bodies, and class definitions are considered blocks.

This made me wonder about indented code sections like what you see inside if statements or while loops. Are these not actually called blocks in Python? I always thought they were blocks too.

Can someone explain what “executed as a unit” really means in this context? Why wouldn’t something like a while loop body count as a block? And if these indented sections aren’t blocks, what’s the correct term for them?

Here’s a simple example of what I’m talking about:

num = 5
if num > 0:
    print("Number is positive")
    result = num * 2
    print(f"Double the number: {result}")

I’m trying to understand the proper terminology for the indented part under the if statement.

The trick is understanding what “executed as a unit” actually means in Python. When docs say modules, functions, and classes are blocks, they’re talking about code that gets compiled and executed with its own symbol table. These create separate execution environments where Python can optimize variable access and manage scope independently.

Your if statement example shows exactly why indented code isn’t a block. The variable result you create inside the if statement? You can still access it after the if finishes - there’s no scope boundary. If Python treated every indented section as a real block, you’d lose access to those variables once you unindent, just like how local variables in functions aren’t accessible outside the function.

This makes more sense when you think about performance. Python can optimize variable lookup within actual blocks because it knows the exact scope boundaries at compile time. Indented sections under control structures just share the same namespace as whatever contains them.

yeah, python’s weird that way! only modules, functions, and classes create new scopes. those indented blocks in if statements? they’re just suites running in the same scope, so variables defined there stick around afterward.

Python’s confusing because it uses “block” differently than most languages. The docs call modules, functions, and classes blocks because they’re complete compilation units with their own namespace and scope. But those indented sections under if statements and while loops? Python’s grammar calls them “suites” - just groups of statements that run together without creating their own namespace. Here’s the difference: when you define a function, Python creates a separate namespace for it. Variables inside live in their own scope. But code inside an if statement? Those variables still belong to the same namespace as the surrounding code. So in your example, that indented code under the if is technically a suite, not a block. The terminology matters when you’re digging into Python’s execution model and how scope actually works.