The __name__ Trick (Run Code Only When Needed) Dry Run in PYTHON

The __name__ Trick (Run Code Only When Needed) is an interactive PYTHON dry run visualizer from the Modules programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

The __name__ Trick (Run Code Only When Needed) Program Code

# calculator.py
def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

# This part runs ONLY when we run this file directly
if __name__ == "__main__":
    print("Testing calculator:")
    print("2+3 =", add(2, 3))
    print("4*5 =", multiply(4, 5))

View the complete Modules programs page.

Program Console The __name__ Trick (Run Code Only When Needed) Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.