pass in Python is a null statement: when it runs, nothing happens. You use it wherever Python requires a statement but you have no code to put there, such as an empty function, class, loop, if branch or except block.
def sync_accounts():
pass # TODO: implement
class ConfigError(Exception):
pass
Both blocks above are valid Python. Remove the pass lines and the file will not even load.
Why Python needs pass
Python marks a block with a colon and an indented body. The body cannot be empty. A comment does not count either, because comments are discarded before the parser looks for a statement. If you write a header with nothing under it, Python refuses to run the file:
def sync_accounts():
# TODO: implement
print("hello")
Example output on a recent Python 3 release:
File "app.py", line 4
print("hello")
^
IndentationError: expected an indented block after function definition on line 1
Adding pass as the body fixes it. The language reference puts it plainly: pass is a null operation, and it is useful as a placeholder when a statement is required syntactically but no code needs to be executed. If you hit this error in other situations, see Python IndentationError explained.
pass in a function
The most common use is a stub: you know a function will exist, but you have not written it yet. The tutorial describes this as a place-holder for a function body "when you are working on new code, allowing you to keep thinking at a more abstract level."
def load_settings(path):
pass
def save_settings(path, settings):
pass
settings = load_settings("settings.json")
print(settings)
Example output:
None
The function runs without error and returns None, as every Python function does when it ends without a return. That is worth remembering: a stub made with pass does not complain when called, so you can forget it is unfinished. If you would rather get a loud failure, raise NotImplementedError in the body instead. For how functions and return values work, see defining a function in Python.
pass in a class
A class with no attributes or methods still needs a body. The tutorial says pass is commonly used for creating minimal classes:
class Empty:
pass
record = Empty()
record.name = "demo"
print(record.name)
Example output:
demo
The most frequent real-world case is a custom exception. You often only need a new name that you can catch separately, and the inherited behaviour is enough:
class ValidationError(Exception):
pass
try:
raise ValidationError("name is required")
except ValidationError as error:
print(error)
Example output:
name is required
When the class does have content, pass is no longer needed. A docstring alone is also a valid body, so a documented empty class does not need it. More on classes in Python classes explained.
pass in if, for and while
You can leave a branch empty while you decide what belongs there:
for order in orders:
if order.is_paid:
pass # nothing to do for paid orders
else:
send_reminder(order)
This works, but it is usually clearer to invert the condition and drop the empty branch:
for order in orders:
if not order.is_paid:
send_reminder(order)
Treat a pass inside an if as a sign to check whether the condition can be rewritten. A pass that stays in finished code should have a comment saying why the branch is intentionally empty.
The tutorial's own loop example is a busy-wait: while True: pass spins until you press Ctrl+C. It keeps a CPU core busy, so it is a demonstration rather than something to copy into real programs.
pass in an except block
This is the use that deserves the most care. An except block with only pass catches the error and throws it away:
import os
try:
os.remove("cache.tmp")
except FileNotFoundError:
pass
Here it is reasonable: you wanted the file gone, and it is already gone. The exception is specific and ignoring it is the correct outcome. Compare this version:
try:
process(record)
except Exception:
pass
Now every failure inside process, including typos and bad data, vanishes without a trace. Catch the narrowest exception that you expect, and log anything you are not certain is harmless. The standard library also offers contextlib.suppress for the first case. Its documentation says it suppresses the specified exceptions in a with block and resumes after it, and warns that it should be used only for very specific errors where silently continuing is known to be right:
from contextlib import suppress
import os
with suppress(FileNotFoundError):
os.remove("cache.tmp")
For the wider picture on catching errors, read Python try and except.
pass vs continue
People mix these up because both appear inside loops and both seem to "skip". They do different things. pass does nothing and execution moves on to the next line. continue abandons the rest of the current iteration and starts the next one.
for number in [1, 2, 3]:
if number == 2:
pass
print(number)
Example output:
1
2
3
for number in [1, 2, 3]:
if number == 2:
continue
print(number)
Example output:
1
3
With pass, 2 is still printed because the print line runs. With continue, the loop jumps past it. There is a fuller treatment in Python continue statement examples.
| Statement | What it does | Typical use |
|---|---|---|
pass | Nothing; execution continues with the next statement | Empty function, class or branch |
continue | Skips the rest of this iteration and starts the next | Filtering inside a loop |
break | Leaves the loop entirely | Stopping a search early |
... (ellipsis) | A constant expression that evaluates and is discarded | Placeholder body by convention |
pass or ... (ellipsis)
You will see both in stubs:
def parse(text): ...
def render(page):
pass
The tutorial addresses this directly: many people use the ellipsis literal ... instead of pass as a placeholder body, but this has no special meaning to Python and is not part of the language definition. You could use any constant expression there. The ellipsis is common in type stubs and protocol definitions, where it reads as "body intentionally omitted". For ordinary code, pass is the more explicit choice, and neither is wrong. Pick one and stay consistent within a project.
Common mistakes
- Leaving a stub in finished code. A function whose body is only
passreturnsNoneand never tells you it is unfinished. Search forpassbefore you ship, or useraise NotImplementedErrorfor stubs that must not be called. - Using pass where continue is meant. If the loop should skip the remaining code for an item,
passwill not do it. The output above shows the difference. - Swallowing broad exceptions.
except Exception: passhides real bugs. Narrow the exception or log it. - Adding pass after real code. It is legal but pointless. If a block already has a statement,
passadds nothing. - Writing pass on the header line for a block with more code.
def f(arg): passon one line is valid for an empty function, as the language reference shows, but readability suffers once the body grows. Put the body on its own indented line.
If you still get an IndentationError
Check that pass is indented one level deeper than the line ending in a colon, and that you use the same indentation style (spaces or tabs) as the rest of the block. A pass at the same indent as the def or if line does not count as its body, and Python raises the same expected an indented block error shown at the top.
Frequently asked questions
- What does pass do in Python?
- pass is a null operation. When Python executes it, nothing happens. It exists because Python requires at least one statement in a block (a function, class, loop, if or except body), and pass lets you write a block that has no code yet or needs none.
- What is the difference between pass and continue in Python?
- pass does nothing and the loop body carries on with the next statement. continue ends the current loop iteration immediately and jumps to the next one, so any code after it in the body is skipped.
- Can I use ... instead of pass in Python?
- Yes. The Python tutorial notes that many people write the ellipsis literal ... as a placeholder body. It has no special meaning to Python and is not part of the language definition, it is just a convention, and pass remains the explicit way to say 'do nothing here'.
- Is it bad practice to use pass in an except block?
- An except block containing only pass silently swallows the error, which hides bugs. It is acceptable for a specific, expected exception where ignoring it is the right behaviour. Catch the narrowest exception you can, and consider contextlib.suppress or a log message instead.