pass statement
pass is a placeholder that does nothing. Use it where Python needs a statement but you have no code to write yet.
passPython uses indentation to mark blocks, and a block cannot be empty. pass fills that gap: it is a real statement that does nothing at all.
You see it in functions and classes you plan to write later, in except blocks where you really mean to ignore an error, and in small classes that only exist to have a name, such as your own exception types.
pass does not skip or stop anything. It is not the same as continue statement or break statement: the code after it runs as usual.
Examples
1A function to finish later
def send_report():
pass
result = send_report()
print("Still runs, result:", result)Still runs, result: None
Without pass, the empty function would be a SyntaxError.
2Your own exception type
The class needs no body: it inherits everything from Exception and only adds a new name.
class OutOfStockError(Exception):
pass
try:
raise OutOfStockError("no red shoes in size 38")
except OutOfStockError as e:
print("Sorry:", e)Sorry: no red shoes in size 38
3pass does not skip anything
for n in range(3):
if n == 1:
pass
print("n is", n)n is 0 n is 1 n is 2
Compare with continue, which would have skipped printing 1.
4Deliberately ignore an error
values = ["4", "x", "9"]
numbers = []
for v in values:
try:
numbers.append(int(v))
except ValueError:
pass
print(numbers)[4, 9]
Only do this when ignoring the error is really what you want. Silent errors are hard to find later.
Common mistakes
Using pass when you mean continue
pass lets the rest of the loop body run. If you want to skip the rest of the round, use continue.
except: pass everywhere
A bare except: pass hides every error, including typos in your own code. Catch the specific error you expect, such as except ValueError:.
Questions people ask
What is the difference between pass and ... (Ellipsis)?
Both can fill an empty body. ... is a value, not a statement, and is often used in type stubs. pass is the classic choice and reads as "nothing here on purpose".
Is pass the same as continue?
No. pass does nothing and the code below it still runs. continue statement jumps to the next loop round.
Can I leave a function body empty with only a comment?
No. A comment is not a statement, so Python still needs pass (or a docstring) in the body.