Concepts / Writing Reusable Functions

Writing Reusable Functions

The __name__ attribute tells you whether a module is being run directly (__main__) or imported (module name).

  • Programming

One File, Two Uses

A Python file can serve two purposes: it can be run as a standalone program, or it can be imported so that another program can use its functions. Reusable design requires these two uses to behave differently when necessary. The __name__ attribute gives the file information about how it is being used, allowing the file to protect test or demonstration code from running during an import.

Run directly__name__ = '__main__'Import module__name__ = module name
What value does __name__ contain when a file is run directly compared with when it is imported?

Tracing the Module Identity

Python automatically sets __name__ according to the way a file is being used. For the file the user runs directly, __name__ is set to the exact string '__main__'. For a file that is imported, __name__ is set to the module's name instead. This value is the signal that lets the file distinguish standalone execution from imported use.

What do you think happens?

Suppose a file named module_using_name is run directly and then imported from another script. What will __name__ contain in each situation?

  • It contains '__main__' in both situations.
  • It contains 'module_using_name' in both situations.
  • It contains '__main__' when run directly and 'module_using_name' when imported.
Reveal answer

Answer: It contains '__main__' when run directly and 'module_using_name' when imported.

Python assigns '__main__' to the file run directly. When another script imports the file, Python assigns the module's name instead.

runs directlysets __name__importssets __name__UserModule file__main__Other scriptmodule name
How does control flow move differently when the file is launched directly versus imported by another program?

The Main Guard

The main guard is the condition if __name__ == '__main__':. It checks whether the file is the program being run directly. If the condition is true, the indented block executes. If the file is imported, __name__ contains the module name instead, so the condition is false and that block is skipped.

python
Output
When this file is run directly:
5

When this file is imported:
The function add is available, but the print statement inside the main guard does not execute.
checkstruefalseModule usage__name__ check__name__ == '__main__'Test codeexecutesTest codeskipped
Which code path executes when the module is run directly, and which path is skipped when the module is imported?

Reusable Definitions and Standalone Tests

A Calculator Module

Design a file that offers add and multiply functions to importing programs while keeping its test output for standalone execution.

Define reusable functions: Place the add and multiply functions in the module so another programmer can import and use them.

Add standalone test code: Place calls that test the functions inside the if __name__ == '__main__': block.

Run the file directly: The file has __name__ equal to '__main__', so the test calls execute and their output is shown.

Import the file: The file has __name__ equal to its module name, so the main-guard condition is false. The functions are available, but the test output does not run.

One file supports both reusable function access and separate standalone testing without automatically producing test output during import.

def add(first, second): return first + second def multiply(first, second): return first * second if __name__ == '__main__': print(add(2, 3)) print(multiply(2, 3))

importimportadd and multiplyreusable definitionsTest callsinside main guardadd and multiplyavailable after importTest callsskipped during import
What becomes available during import, and what runs only when the file is executed as a standalone program?

Exact Syntax Matters

  • Using one underscore instead of the double underscores in __name__.

    The pattern depends on the __name__ attribute. Changing its spelling does not perform the intended check.

    Fix: Write __name__ with two underscores before name and two underscores after it.

  • Comparing against a different string.

    The direct-execution value is the exact string '__main__'.

    Fix: Compare __name__ to '__main__', including both underscores.

  • Putting standalone test code outside the guard.

    That code is not protected by the condition, so it is not separated from the reusable module code.

    Fix: Indent standalone test or demonstration calls beneath if __name__ == '__main__':.

Practice the Decision

MEDIUM

A file contains a reusable function and a print statement inside if __name__ == '__main__':. Predict what happens in these two cases: first, the file is run directly; second, another script imports it. State whether the function is available and whether the print statement executes in each case.

Hints
  • Start by identifying the value of __name__ in each situation.
  • The condition is true only when __name__ equals '__main__'.
  • The source material states that imported functions remain available while protected test output does not run.
  1. For direct execution, assign __name__ the value '__main__'.
  2. For import, assign __name__ the module's name instead.
  3. Evaluate the main-guard condition in each case.
  4. Separate the reusable function from the protected print or test code.

Reusable Module Checklist

  1. When a file is run directly, Python sets __name__ to '__main__'.
  2. When a file is imported, Python sets __name__ to the module's name.
  3. The condition if __name__ == '__main__': runs its indented block only during direct execution.
  4. Reusable functions can be made available to importing programs without automatically running standalone test or demonstration output.
  5. The exact double-underscore spelling and the exact string '__main__' are required.

Use the main guard when a file is intended to work both as a reusable module and as a standalone program. This pattern keeps test or demonstration code from cluttering programs that import the module and signals that the file has both uses.

Key Takeaways

  • The __name__ attribute identifies whether a file is being run directly or imported.
  • Direct execution sets __name__ to '__main__'; importing sets it to the module's name.
  • The main guard protects standalone test and demonstration code.
  • Functions can remain available for import while protected output is skipped.
  • Always use the exact syntax if __name__ == '__main__':.