Python Reference ยท Statements and Keywords
The pass statement
Reference for Python's pass statement: a do-nothing placeholder for empty blocks, stub functions and classes, silent except clauses, and pass versus the ellipsis.
pass is a statement that does nothing. Python needs at least one statement in every indented block, so when a block has no work to do yet (or deliberately never will), pass fills the space and keeps the syntax valid.
It is typically used for stubs: functions and classes you plan to write later, empty exception classes, and except branches that intentionally ignore an error.
def todo(): pass
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
pass
Return value: none. The statement has no effect and generates almost no code.
Usage notes
- Where it helps: empty function and class bodies while you sketch a design, branches of
if/elifthat need no action,exceptclauses that ignore an error, and abstract or marker classes. - It is not
continue.passdoes nothing and the loop body carries on with the next statement;continueskips the rest of the iteration. It is also notbreak. - Not a comment. Unlike
#,passis executable (though free). It only exists to satisfy the grammar. - Alternatives: a docstring alone makes a valid body, so documented stubs rarely need
pass. The ellipsis literal...is common for stubs and intyping.Protocolor@overloaddefinitions. For unimplemented functionality,raise NotImplementedErroris safer than silently returningNone. - Silent
except: passhides bugs. Catch the narrowest exception, and think about logging it.contextlib.suppress(...)states the intent clearly. - Needed after a colon only when nothing else follows. A block with any other statement does not need
pass. - A common beginner confusion:
passinside a loop orifdoes not "do something later"; if you remove it, the code simply will not parse unless another statement takes its place.
Related
if,def,class,trybreakandcontinue,contextlib- Lessons: Syntax, Custom exceptions