Pass Statement
Python · Control Flow
Theory
pass is a statement that does nothing. It exists because Python's grammar requires every block, the body of an if, for, while, def, class or try, to contain at least one statement. A block holding only a comment is not enough, since comments are thrown away before compiling, so it raises IndentationError: expected an indented block. Writing pass satisfies the grammar while doing no work, and the program simply moves on, which is why the example prints done.
Typical uses are placeholders while you sketch the structure of a program (def load_data(): pass), empty classes such as custom exceptions (class InvalidOrder(Exception): pass), and deliberately ignoring a case. Do not confuse it with continue or break: pass has no effect on loops at all, continue jumps to the next iteration and break leaves the loop. An empty block that silently swallows errors, like except: pass, hides bugs, so use it with care.
Why we use it
While writing code top-down, you often know a function or branch is needed before you know what it should do. pass lets the program run and be tested now, with the empty pieces filled in later.
When to use it
Use pass as a temporary placeholder or for intentionally empty classes and blocks. Use continue to skip to the next loop iteration and break to exit a loop. A docstring or ... (Ellipsis) also works as a placeholder body in functions.
How it works
- Python reaches if status == 'pending':
- Is the condition true? Yes: Enter the block. No: Skip the block.
- Execute pass: nothing happens
- Leave the block normally
- Continue: print('done')
Syntax
if condition:
passExample
status = "pending"
if status == "pending":
pass # TODO: handle later
print("done")Key points
- Common in stub functions/classes while still under development
- Does nothing at runtime; purely a placeholder
- A block with only a comment is an IndentationError; pass fixes it
- pass does not skip loop iterations; that is continue
- Avoid except: pass, which silently hides errors