< previous | Table of Contents | next > |
3.4 THE PASS STATEMENT
The pass
is a statement that does nothing. This can be useful in various situations, especially in places where your code will eventually go but has not been written yet (a placeholder) or when you're required to have a statement by Python's syntax but you don't want to execute any code.
PASS STATEMENT AS A PLACEHOLDER
The most common use of pass
is as a placeholder for future code. It's common to outline your code's structure during the development process without implementing the functionality immediately. Using pass
allows you to run your code without causing any errors.
has_ID_card = True if has_ID_card: pass # Placeholder for implementing further logic later. else: print("You need an ID card to proceed!")
Python syntax requires at least one statement in blocks like if
, elif
, else
, for
, while
, and except
. If you have a block with no content, you can use pass
to avoid an error.
If we do not include a pass statement but insert a comment instead in an if statement, for example, the result will be an IndentationError
. Here's a Thonny example to illustrate.
Furthermore, for exception handling, you may want to catch exceptions but intentionally ignore them without performing any error handling, so you can use the pass
statement in the except block. More on exception handling will be discussed at the end of this chapter.
KEY THINGS TO REMEMBER
- The
pass
statement is a null operation that does nothing when executed. It's effectively a placeholder in the code. - It helps prevent errors by fulfilling Python's requirement for non-empty code blocks.
< previous | Table of Contents | next > |