Python has a special keyword called
pass. The Python
pass keyword tells Python to do nothing at all. In other words: just pass this line of code and continue. If you are used to programming in C-like languages, you probably never needed such a statement.
- So why does this keyword exist in Python and not in other languages?
- And where and how would you use it?
This article answers these questions and shows you several situations in which Python’s
pass keyword is needed. We’ll also look at an interesting alternative you might like to use instead!
Table of Contents
What is the pass keyword
pass is a keyword, just like
return, and others are keywords.
pass doesn’t do anything. It’s a keyword that tells Python to continue running the program, ignoring this line of code.
pass is often used as a placeholder for future code.
Note that because
pass forms a valid statement on its own, pass can be called a statement too. Hence, you’ll see people using the words pass statement and pass keyword, and it’s both correct.
Why does Python need a pass keyword?
Python relies on indentation. Python can only detect a code block if it is indented with an equal amount of white space (usually four space characters). Python grammar is defined in such a way that code is required in some places. This is done because not adding code in these places would not make much sense and would not help with readability.
Let’s take a look at four places where you must have code. There might be more (please let me know), but these are the most prominent ones. If you don’t enter code in these places, Python will exit the program with an
Let’s try some of these for the sake of demonstration:
def myfunction(): # This function has no body, which is not allowed myfunction()
Python will fail to run this code with the following error:
File "myfile.py", line 4 myfunction() ^ IndentationError: expected an indented block after function definition on line 1
Similar complaints will result in the other cases:
if True: # No code, not allowed! else: # Here too, code is required try: # let's try doing nothing (not allowed) except Exception: # Again: this can't be empty! class MyClass: # A completely empty class is not allowed
As you can see, a comment is not considered code, so comments can’t be used to fill in the gap! This is where the pass statement comes into play. In all the situations above, the pass statement can be used to insert code that does nothing but still satisfies the requirement of having some code in place. Let’s fill in the gaps:
def myfunction(): pass myfunction() if True: pass else: pass try: pass except Exception: pass class MyClass: pass
This is all valid but completely useless code.
When to use Python’s pass keyword
So when should you use
pass? There are several reasons, and I think these are the most common ones.
As a placeholder
As mentioned before,
pass can be useful during development. We can use it to create stub functions and classes that will be implemented later on. This way we can create runnable code that doesn’t throw errors, even while not all the functions, classes, error handling, and if-else cases are implemented.
It’s very common to ignore exceptions, especially when they are
KeyErrors. This is a pattern you’ll see often:
try: email = customer['email'] send_notification(email, ...) except KeyError: # Not the end of the world, let's continue pass
We catch the error but don’t want to do anything with it. However, Python requires code in an
except block, so we use
Create a breakpoint while debugging
Another interesting use of
pass is to create breakpoints in your code. Sometimes you want to create a breakpoint right after some code. If that code is at the end of a function, you might want to throw in an extra
pass at the end and set your breakpoint there. To learn more about breakpoints, make sure to read my article on debugging Python code in VSCode and the article on using the Python debugger.
Creating subclasses with no extra functionality
If you want to create a subclass without adding functionality, you can use
pass to do so. This can come in handy if you want to create a custom-named exception, for example:
class MyCustomException(Exception): pass
When not to use pass
In my opinion, you should use the
pass keyword very sparingly in production code. And if you do, it should be incidental. As mentioned before, using
pass to ignore an exception is obviously OK. But should you ship production code full of stubs using
pass? I’m not sure!
If you find yourself using the pass keyword a lot, e.g., in
else constructs, you may want to try rewriting your code. Let’s look at an example:
if customer_exists: pass else: redirect_to_signup_page()
In the above example, we check if a customer exists. If it does, we’re all set and we use
pass to continue. If not, we redirect to a sign-up page. Code like this can be rewritten easily though, resulting in fewer lines of code and less complexity. Often, all you need to do is invert the logic:
if not customer_exists: redirect_to_signup_page()
We saved ourselves two lines of code and made the code more readable.
An alternative to using pass
I’ve written a blog article on a special object in Python called the Ellipsis. In that article, I mentioned that we can use the Ellipsis to replace
pass. The ellipsis object doesn’t do anything, hence it can be used exactly as you would use
pass. Here’s an example:
def create_account(username, password): # TODO: implement me ...
Instead of using
pass, I used the three dots
... that form an ellipsis object. Looks pretty cool, if you ask me, and it almost begs to be replaced by some actual code!
It doesn’t stop here, though. You can put any statement without side effects in spots where you would otherwise put
pass, here are some possibilities:
- A number, like
- A boolean (
- A string: “Not implemented yet”
Despite all these alternatives,
pass is the de-facto standard in these cases and clearly communicates to others that this is intentionally left blank, or needs to be implemented. So when in doubt, use
pass, especially when you’re working in a team.
Python pass vs continue
Many people asked me what the difference is between
continue. They might look the same, but they are far from the same. While
pass does nothing at all,
continue basically stops the current flow of code and directly starts the next iteration of a loop. Hence,
continue only works inside loops and it can not be used as a replacement for
Here’s how you would use
continue in a for-loop:
# Send voucher to all inactive customers for customer in customers: if customer.is_active(): # Skip this customer continue # do some stuff to send voucher ...
We learned what Python’s pass keyword is, why Python needs the
pass keyword, and in which cases you should and shouldn’t use it. We even looked at a pretty cool alternative: the ellipsis. Finally, I explained the difference between