pass
The pass keyword is a null operation in Python. It does absolutely nothing — it’s a placeholder that tells Python “do nothing here.” It’s primarily used when you need syntactically valid code but don’t want to execute any logic.
Syntax
pass
That’s it — just the word pass on its own line (or as the body of a block).
Why It Exists
Python uses indentation to define code blocks. In languages like C or JavaScript, you can write an empty block like this:
// C
if (condition) {
// do nothing
}
But in Python, you can’t have an empty block — the interpreter would raise an error:
# This raises IndentationError
if condition:
That’s where pass comes in:
# Perfectly valid Python
if condition:
pass
Common Use Cases
Placeholder in conditionals
def process_data(data):
if not data:
# TODO: implement later
pass
else:
return transform(data)
Abstract methods
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def speak(self):
"""Every animal must have a sound."""
pass
class Dog(Animal):
def speak(self):
return "Woof!"
# This works - pass satisfies the abstract method requirement
dog = Dog()
print(dog.speak())
# Woof!
# This raises TypeError - can't instantiate abstract class
# animal = Animal() # TypeError
Class placeholders
# TODO: implement User class
class User:
pass
This creates a valid class you can reference, even if it has no attributes yet:
User = type("User", (), {})
isinstance(User(), User)
# False
class User:
pass
isinstance(User(), User)
# True
Function placeholders
def unimplemented_feature():
"""This feature is coming soon."""
pass
Loops
for i in range(10):
if i == 5:
print("Found five!")
break
# Do nothing for other iterations
pass
Exception handling
try:
risky_operation()
except ValueError:
# We don't care about ValueError, ignore it
pass
except Exception as e:
# Log other errors but keep running
log_error(e)
pass
pass vs Ellipsis (…)
Python has another “do nothing” value — the ellipsis ... (three dots):
# Both are valid placeholders
if True:
pass
if True:
...
The difference:
passis a statement — it’s a complete Python keyword...is an expression — it’s an object (of typeEllipsis)
print(...) # Ellipsis
print(type(...)) # <class 'ellipsis'>
x = ...
print(x) # Ellipsis
Ellipsis is useful for type hints and numpy array slicing:
from typing import Union
# Type hint with ellipsis for "any type"
def process(items: list[any]) -> None:
pass
# NumPy array slicing
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[...]) # [1 2 3 4 5]
For empty code blocks, pass is the conventional choice.
Real-World Example
A common pattern in game development:
class GameState:
def handle_input(self, key):
pass # Override in subclasses
def update(self):
pass
def render(self):
pass
class Playing(GameState):
def handle_input(self, key):
if key == "escape":
return MenuState()
return self
def update(self):
# Update game logic
pass
def render(self):
# Draw game
pass
See Also
- def keyword — define functions
- class keyword — define classes
- break keyword — break out of loops