Python pass (Do Nothing): When And How To Use

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!

What is the pass keyword

Python’s pass is a keyword, just like if, else, 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 IndentationError exception:

  1. Function definitions
  2. Class definitions
  3. if… else statements
  4. try… except… finally statements

Let’s try some of these for the sake of demonstration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def myfunction():
# This function has no body, which is not allowed
myfunction()
def myfunction(): # This function has no body, which is not allowed myfunction()
def myfunction():
    # This function has no body, which is not allowed

myfunction()

Python will fail to run this code with the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
File "myfile.py", line 4
myfunction()
^
IndentationError: expected an indented block after function definition on line 1
File "myfile.py", line 4 myfunction() ^ IndentationError: expected an indented block after function definition on line 1
  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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def myfunction():
pass
myfunction()
if True:
pass
else:
pass
try:
pass
except Exception:
pass
class MyClass:
pass
def myfunction(): pass myfunction() if True: pass else: pass try: pass except Exception: pass class MyClass: pass
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.

Ignoring exceptions

It’s very common to ignore exceptions, especially when they are KeyErrors. This is a pattern you’ll see often:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
try:
email = customer['email']
send_notification(email, ...)
except KeyError:
# Not the end of the world, let's continue
pass
try: email = customer['email'] send_notification(email, ...) except KeyError: # Not the end of the world, let's continue pass
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 pass.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class MyCustomException(Exception):
pass
class MyCustomException(Exception): pass
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 ifelse constructs, you may want to try rewriting your code. Let’s look at an example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if customer_exists:
pass
else:
redirect_to_signup_page()
if customer_exists: pass else: redirect_to_signup_page()
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if not customer_exists:
redirect_to_signup_page()
if not customer_exists: redirect_to_signup_page()
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def create_account(username, password):
# TODO: implement me
...
def create_account(username, password): # TODO: implement me ...
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 0
  • A boolean (True or False)
  • 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 pass and 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 pass.

Here’s how you would use continue in a for-loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Send voucher to all inactive customers
for customer in customers:
if customer.is_active():
# Skip this customer
continue
# do some stuff to send voucher
...
# Send voucher to all inactive customers for customer in customers: if customer.is_active(): # Skip this customer continue # do some stuff to send voucher ...
# Send voucher to all inactive customers
for customer in customers:
    if customer.is_active():
        # Skip this customer
        continue
    
    # do some stuff to send voucher
    ...

Conclusion

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 pass and continue.

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Related articles

Leave a Comment