Control Flow
Pass Statement
Placeholder for empty blocks
Interview: Python-specific concept — tests understanding of syntax requirements and code design
The pass statement is a null operation — it does nothing when executed. It exists because Python's syntax requires at least one statement in any indented block (function body, class body, if/elif/else, loop, try/except). Without pass, you'd get an IndentationError for empty blocks.
When to Use pass
- Stub functions: Define function signatures before implementing logic (top-down design)
- Abstract base classes: Define interface methods that subclasses must override
- Custom exceptions: Create exception classes that add no new behavior
- TODO placeholders: Mark blocks you plan to implement later
- Minimal except blocks: When you intentionally ignore specific exceptions (use sparingly)
- Empty class bodies: Marker classes or protocol definitions
Pass vs Alternatives
passvs...(Ellipsis): Ellipsis is functionally equivalent and preferred in type stubs (.pyifiles)passvsraise NotImplementedError: Use raise when the method MUST be overridden; pass silently does nothingpassin except: Bareexcept: passis an anti-pattern — always catch specific exceptions and add a comment explaining why
Best Practice
Always pair pass with a # TODO comment explaining what should go there. A bare pass is a code smell — it tells future developers (including yourself) nothing about intent.
Pass in Class Hierarchies
Pass is commonly used to create custom exception classes and abstract interfaces:
- Custom exceptions:
class AppError(Exception): pass - Marker interfaces:
class Serializable: pass - Protocol stubs during development before adding actual method signatures
Use Cases
Defining stub functions during top-down program design
Creating custom exception class hierarchies
Abstract base class method placeholders
TODO markers in if/else branches during incremental development
Common Mistakes
Using bare except: pass which silently swallows all exceptions including SystemExit
Leaving pass without a TODO comment — future developers cannot understand intent
Using pass in a while loop body instead of time.sleep() (creates 100% CPU busy-wait)
Using pass instead of raise NotImplementedError for methods that MUST be overridden