How to Define Unimplemented Functions in Python: pass and NotImplementedError

In the early stages of development, you often want to define just the function or class names to design the overall structure, intending to implement the logic later. However, since Python manages blocks using indentation, leaving a function body completely empty causes an IndentationError, and the program will not run.

This article explains how to use the pass statement to define a function that “does nothing” and NotImplementedError to explicitly notify that a feature is not yet implemented at runtime.


目次

1. The pass Statement: Explicitly Do Nothing

pass is a syntax used in Python when “you need to write something grammatically, but you don’t want to execute any processing.” By writing this, you can define empty functions or classes without errors.

Syntax:

def function_name():
    pass

Specific Example

Let’s assume the design phase of a data analysis tool. We will define three functions: load, process, and save. We create only the framework, and the contents are empty for now.

def load_raw_data(file_path):
    """
    Function to load file (To be implemented)
    """
    pass

def process_data(data):
    """
    Function to process data (To be implemented)
    """
    pass

def save_report(result):
    """
    Function to save report (To be implemented)
    """
    pass

# --- Main Processing ---
print("Starting process.")

# Calling functions causes no errors; nothing happens and it finishes
load_raw_data("data.csv")
process_data(None)
save_report(None)

print("Process complete.")

Output:

Starting process.
Process complete.

As shown, by using pass, you can run the entire program even if the content is empty. It is very useful as a placeholder during development.


2. NotImplementedError: Notify Error at Runtime

The pass statement simply skips processing. However, sometimes you want to explicitly raise an error saying, “This feature is not usable yet.” In that case, instead of pass, you raise a NotImplementedError exception.

Syntax:

def function_name():
    raise NotImplementedError("This feature is not implemented yet.")

Specific Example

Here is an example that raises an error when a “cloud sync function” (planned for the future) is called.

def sync_to_cloud():
    # Not implemented yet, so raise an error if called
    raise NotImplementedError("Cloud sync feature is under development.")

print("Starting sync process...")

try:
    sync_to_cloud()
except NotImplementedError as e:
    print(f"Error: {e}")

print("Process interrupted.")

Output:

Starting sync process...
Error: Cloud sync feature is under development.
Process interrupted.

This ensures that if you forget to implement a function and try to use it, you will notice it immediately.


3. Using Ellipsis (...)

In Python 3, you can also write ... (Ellipsis) instead of pass. Functionally, it is almost the same as pass, doing nothing. It is a notation often seen in Type Hint stub files.

def calculate_metric(value):
    ...  # Same meaning as pass, does nothing

calculate_metric(100)

In standard development, pass is more common, but ... is used when you want to write more concisely.


Summary

To define unimplemented functions or classes in Python, use the following methods depending on the situation:

  • pass statement: When you want to create just a framework and run the code without errors (Most common).
  • raise NotImplementedError: When you want to raise an error if the function is called, indicating it is “unimplemented.”
  • ... (Ellipsis): When you want to write it more concisely than pass.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次