Menu
CoddyTech

Imports Are Not Includes

Lesson 29 of 48 in Coddy's Python for C Developers course.

C #include inserts header text during preprocessing, and the linker joins compiled definitions. Python import loads a module and executes its top-level statements on first import in a process, normally caching the module for later imports. A .py file can therefore expose helpers and also run as a script. Define main outside the guard and call it under if __name__ == "__main__" so an importer can use functions without reading input or printing. A Python import is not a textual header inclusion.

C: complete example.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int square(int value) { return value * value; }
int main(void) {
    printf("%d\n", square(4));
    return 0;
}

Python: complete example.py

def square(value):
    return value * value
def main():
    print(square(4))
if __name__ == "__main__":
    main()

Watch the difference

All tasks contain multiple files. Use from module_name import function_name without the .py suffix. The locked driver is the entry file; reusable modules should not accidentally run their interactive path when imported.

Your code goes in program.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.

challenge icon

Challenge

Beginner

In program.py import math and implement common_pack(a, b) returning math.gcd(a,b). Define main() outside the guard; it reads two nonnegative integers and prints their gcd. Zero paired with a number yields that number; two zeros yield zero. Call main() only under if __name__ == "__main__":. The locked driver consumes a script/import mode first. In script mode your main reads the two numbers. In import mode the driver imports program, rejects input/output during import, then reads a count and that many integer pairs and prints helper results. Your code must not read the mode or helper-call count.

C #include is preprocessing; Python import executes top-level module statements. Keep input/output behind the main guard.

Your code goes in program.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.

Try it yourself

import importlib as _coddy_importlib
import runpy as _coddy_runpy
import builtins as _coddy_builtins
_coddy_input = _coddy_builtins.input
_coddy_print = _coddy_builtins.print
_coddy_mode = _coddy_input()
_coddy_import_io = []
def _coddy_forbidden_input(*args, **kwargs):
    _coddy_import_io.append("input")
    raise AssertionError("input during module loading")
def _coddy_forbidden_print(*args, **kwargs):
    _coddy_import_io.append("print")
    raise AssertionError("print during module loading")
if _coddy_mode == "script":
    __name__ = "__main__"
elif _coddy_mode == "import":
    __name__ = "course_module"
    _coddy_builtins.input = _coddy_forbidden_input
    _coddy_builtins.print = _coddy_forbidden_print
else:
    raise AssertionError("invalid harness mode")

if _coddy_mode == "script":
    _coddy_namespace = _coddy_runpy.run_module("program", run_name="__main__")
else:
    _coddy_namespace = vars(_coddy_importlib.import_module("program"))
common_pack = _coddy_namespace['common_pack']
main = _coddy_namespace['main']
_coddy_builtins.input = _coddy_input
_coddy_builtins.print = _coddy_print
if _coddy_import_io:
    raise AssertionError("module loading attempted input or output")
if _coddy_mode == "import":
    if not callable(main):
        raise AssertionError("main must be a callable function")
    _coddy_count = int(_coddy_input())
    for _coddy_index in range(_coddy_count):
        _coddy_left, _coddy_right = _coddy_input().split()
        _coddy_result = common_pack(int(_coddy_left), int(_coddy_right))
        if type(_coddy_result) is not int:
            raise AssertionError("common_pack must return an integer")
        _coddy_print(_coddy_result)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for C Developers

Practice on your own: Online Python compiler