Post

Python pass function

In this tutorial, we will learn all about the python pass function.

Python pass function

What is Python pass function

Pass in python is used as a placeholder for future statements and code. Python pass function the null statement which does nothing in python. The null statement will return nothing or null when we execute a pass statement.

The pass statement is useful when you are dealing with very large code when you have to work on multiple conditional statements, loops, or in-class and custom functions.

Python is a language that enforces indentation and block structure. Unlike some other languages where you can leave an empty block with just a pair of curly braces {}, Python requires at least one statement inside every block. This is where pass becomes invaluable. Without it, Python would throw an IndentationError or SyntaxError whenever you tried to leave a code block empty.

Think of pass as a “TODO” marker that keeps your code syntactically valid while you are still working on it. It is especially popular in large codebases where developers write the structure (skeleton) of the application first and then fill in the actual logic piece by piece.

Syntax

The syntax of the pass statement is:

1
pass

It is simply the single keyword pass. No parentheses, no arguments — just the word itself on its own line inside a block.

How pass Works Internally

When the Python interpreter encounters the pass statement, it does absolutely nothing — it does not print anything, does not return any value, does not modify any variable, and does not raise any exception. Execution simply moves on to the next line of code. This behaviour makes it a true no-op (no operation) statement.

Let’s see an example of a pass statement with an if..else conditional statement.

1
2
3
4
if 1 < 5:
    pass
else:
    print("End of if condition")

Output:

(No output is produced)

As we can see, it is not giving any result when the condition is true. Because we are using the pass statement, execution silently moves on without doing anything when the if block runs.

Now compare what would happen without pass:

1
2
3
4
5
# This would raise a SyntaxError:
if 1 < 5:
    # nothing here
else:
    print("End of if condition")

Python cannot handle an empty block, so the pass statement is the correct way to express “do nothing here intentionally.”


Pass statement with for loop

1
2
3
4
5
6
for letter in 'Python': 
   if letter == 'h':
      pass
      print("This is pass block")
   print("Current Letter :", letter)
print("Good bye!")

When we execute the program we will get the output:

1
2
3
4
5
6
7
8
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!

As we can see, we are printing letters using a for loop and we have given the condition that if letter equals 'h', the statements inside the if block should be executed. When the condition becomes true, Python calls the pass statement (which does nothing) and then executes the print statement that follows it. Notice that unlike continue, pass does not skip the rest of the block — it simply acts as an empty statement and the remaining code in the block continues to run normally.

Difference Between pass and continue in a Loop

A common point of confusion is the difference between pass and continue:

1
2
3
4
5
6
7
8
9
10
11
# Using pass — the print still runs
for letter in 'Python':
    if letter == 'h':
        pass          # does nothing, loop body continues
    print("Letter:", letter)

# Using continue — the print is skipped for 'h'
for letter in 'Python':
    if letter == 'h':
        continue      # skips to the next iteration
    print("Letter:", letter)

With pass, the output includes Letter: h. With continue, that line is skipped entirely. Understanding this difference prevents subtle logic bugs.


Pass statement with the while loop

As we know there are two main loop types in Python, so just like for loop we can also use pass with a while loop.

1
2
3
count = 0
while count < 5:
    pass   # placeholder — logic to be added later

A more practical example is creating an intentional infinite placeholder loop during early development:

1
2
3
# Skeleton of a server event loop — logic to be added later
while True:
    pass

This keeps the structure intact so you can test surrounding code without having implemented the loop body yet.

Another realistic example — ignoring a specific condition:

1
2
3
4
5
6
7
8
numbers = [1, 2, 3, 4, 5, 6]
index = 0
while index < len(numbers):
    if numbers[index] % 2 == 0:
        pass   # even numbers: no special action for now
    else:
        print(f"Odd number found: {numbers[index]}")
    index += 1

Output:

1
2
3
Odd number found: 1
Odd number found: 3
Odd number found: 5

Pass statement with functions

We can use pass statements in custom functions as well. This is one of the most common uses of pass in professional Python code.

1
2
def function_name():
    pass

A realistic use case is when you are building an API or a module and want to define all the function signatures first before writing any implementation:

1
2
3
4
5
6
7
8
def calculate_tax(income):
    pass

def generate_report(data):
    pass

def send_email(recipient, message):
    pass

All three functions are syntactically valid and callable right now — they just return None. You can write tests for them, wire them into other parts of your code, and fill in the implementation one by one. This is sometimes called the stub-driven development approach.

Abstract Method Pattern

pass also appears frequently when defining abstract-like methods in a base class:

1
2
3
4
5
6
7
8
9
10
11
class Animal:
    def speak(self):
        pass   # Each subclass must override this

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

Pass statement with class

Just like a function, we can use a pass statement in a class definition.

1
2
class ClassName:
    pass

This is useful for creating empty classes that serve as data containers or markers:

1
2
3
4
5
class DatabaseConfig:
    pass   # attributes will be added later

class PermissionError(Exception):
    pass   # custom exception with no extra behaviour needed

The second example above is especially common in Python projects — creating a custom exception class by inheriting from a built-in exception and using pass means the new exception behaves exactly like the parent but has a distinct name that makes error handling clearer.


Real-World Applications of pass

  1. Skeleton code during initial development — Write all your classes and function signatures with pass bodies, then implement them one by one.
  2. Custom exception classesclass MyError(Exception): pass is a complete, working custom exception.
  3. Interface-like base classes — Define the shape of a class without forcing any default behaviour.
  4. Debugging — Temporarily replace a block of code with pass to skip its execution without deleting it.
  5. Conditional no-ops — When one branch of an if/elif/else chain genuinely needs to do nothing.

Common Mistakes and Fixes

Mistake 1: Confusing pass with return

1
2
3
4
5
6
# Wrong expectation
def get_value():
    pass   # This returns None, not "nothing happens and we wait"

result = get_value()
print(result)   # Prints: None

Fix: If you need a function to explicitly signal “not implemented,” raise NotImplementedError instead:

1
2
def get_value():
    raise NotImplementedError("This method must be implemented by subclasses.")

Mistake 2: Confusing pass with continue

1
2
3
4
5
# Intending to skip even numbers but using pass
for n in range(1, 6):
    if n % 2 == 0:
        pass          # Does NOT skip — the print still runs
    print(n)          # Prints 1, 2, 3, 4, 5 (all numbers)

Fix: Use continue when you want to skip the rest of the loop body:

1
2
3
4
for n in range(1, 6):
    if n % 2 == 0:
        continue      # Skips the print for even numbers
    print(n)          # Prints 1, 3, 5

Mistake 3: Forgetting pass in empty try/except blocks

1
2
3
4
5
6
7
8
9
10
11
# SyntaxError — empty except block
try:
    risky_operation()
except ValueError:
    # handle later — this alone causes SyntaxError

# Correct
try:
    risky_operation()
except ValueError:
    pass   # silently ignore for now

Tips and Best Practices

  • Use pass sparingly and always add a comment explaining why the block is empty, so future readers (and future you) understand it is intentional.
  • Prefer raise NotImplementedError over pass inside methods that are genuinely meant to be overridden — it gives a helpful error if someone forgets to implement the method.
  • When using pass in an except block, be cautious. Silently swallowing exceptions can hide bugs. Leave at minimum a # TODO: handle this properly comment.
  • In Python’s typing and Protocol world, pass is standard inside @abstractmethod stubs and Protocol class bodies.

Frequently Asked Questions

Q1: Is pass the same as None?

No. pass is a statement (an instruction to the interpreter) while None is a value (an object). When a function ends with only a pass statement and no explicit return, it implicitly returns None. But pass itself is not a value — you cannot assign it to a variable or pass it as an argument.

Q2: Can I use pass in an except block to ignore exceptions?

Yes, and this is a valid pattern:

1
2
3
4
try:
    int("abc")
except ValueError:
    pass   # We know this might fail and we don't care

However, this should be done deliberately and documented. Blindly ignoring exceptions is one of the most common sources of hard-to-find bugs in Python programs.

Q3: What is the difference between pass and an ellipsis (...) as a placeholder?

Both are used as placeholders, but there is a subtle difference. ... (the Ellipsis object) is more commonly used in type stubs (.pyi files) and abstract method bodies in modern Python, while pass is the traditional placeholder. Functionally, both produce the same result when used alone in a code block, but pass communicates “this block intentionally does nothing” more clearly to most Python readers.

1
2
3
4
5
def method_one():
    pass   # traditional Python placeholder

def method_two():
    ...    # common in type annotations and stubs

Both are perfectly valid; pass is slightly more readable for beginners.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.