Python pass statement: When, why, and how to use it
The pass statement does nothing in Python, which is helpful for using as a placeholder in if statement branches, functions, and classes. In layman’s terms, pass tells Python to skip this line and do nothing.
To demonstrate how pass works, let’s write a small script to iterate through the numbers from one to seventy and then print out any multiples of seven.
pass is a control statement
if , while and for statements are fundamental to all Python programs. These statements can be influenced by what are known as control statements, allowing you to govern your code in different ways. The three control statements in Python are pass , continue and break .
This article looks specifically at the pass statement.
Why use pass ?
As mentioned previously, pass is usually used as a placeholder for branches, functions, classes. Whenever Python arrives at a pass statement, it passes straight over it (hence the name).
This functionality may seem pointless, but let’s try and run our example from the introduction again, without the pass statement:
This code gives us IndentationError: expected an indented block which we’re getting because we haven’t added anything to our if statement.
A codeless statement like this is known as an empty suite. We can avoid this error by using pass as a placeholder for our elif statement as seen in the introduction.
The addition of pass means that Python does nothing in situations where the number variable isn’t a multiple of 27. Despite this seeming redundant, it prevents Python from throwing an error. The flowchart shown below helps demonstrate how pass is working here:
If pass doesn’t do anything, why bother using it at all? To avoid getting an empty suite error, you could delete the code block causing the problem, removing the need for a pass statement.
When writing scripts in Python, there are often situations where you’ll need to code the general layout of a script first, then come back to fill in the blanks later. Using the pass statement in situations like this lets you focus on the structure by writing the code branches first and then using pass as a placeholder to prevent errors from occurring, allowing you to test how the overall script is working before worrying about more minor details.
More examples
Usage in functions
Another example of how pass can be helpful like this is when writing functions. Let’s say we’d like to write a script that takes two user-input numbers then performs some calculations on them. We could write a function perform_calculations to handle the math.
Before filling out the function, we’d like to test if our user inputs are getting recorded correctly:
Once again, we’re getting an error because of an empty suite ( perform_calculations doesn’t have any code). By adding in pass , we can avoid the error:
Seeing as our script is running successfully, we can now worry about filling in our function:
def perform_calculations(a, b): print(f" plus is ") print(f" minus is ") print(f" multiplied by is ") print(f" divided by is ") a = int(input('Enter the first number: ')) b = int(input('Enter the second number: ')) perform_calculations(a, b)
Even though we omitted pass from our final script, it was handy for testing.
Class architecture placeholders
Similarly, pass can be great for designing our class architecture when writing a custom class.
By using pass as a placeholder, we can create a class’ methods without writing the code for them. For an example of this, let’s look at what a custom class for creating the player character in a simple video game might look like:
At the moment, the class has only got one instance method, take_damage, which subtracts ten from the value of the player’s health attribute.
We may not have time to write them right now, but we could add a few other methods to our GameProtagonist class related to abilities the player can use:
We’ve now added in three more methods, attack, heal and fireball. By using pass here, we’ve essentially created a to-do list of things to add to our GameProtagonist class at a later point in time.
When working on large scripts, you may find that your pass placeholders are kept in place for a long time before you’re able to come back and fill in any empty suites, making pass statements extremely useful.
Summary
pass is a great way of exercising more controls over our scripts, hence why it’s called a control statement. Whenever pass is triggered, Python will skip over it, not doing anything. This functionality is beneficial for testing and debugging, as we can use pass as a placeholder.
By adding pass to empty suites, we can prevent scripts from crashing, allowing us to see how our overall scripts are running without needing to finish coding every aspect of a script first.