Main block (`if __name__ == "main"`)
Module 1 — What is if __name__ == "__main__"?
A switch that distinguishes between "executed directly" and "imported".
Definition
Every Python file has a built-in variable __name__. Will the file
direct executed (python meine_datei.py), contains
__name__ the value "__main__". However, the file is from another file imported, contains __name__ the module name.
With the check
if __name__ == "__main__":
<Skript-Code>
you separate cleanly between Library use (Functions are imported) and Script use (the program should start). If someone imports your file, the main block runs not — but the functions are available.
Example 1.1 — Output __name__
Loading Python…Tests
Module 2 — Building a clean file
Functions above, process in the main block.
Recommended layout
A well-structured Python file consists of two parts:
- Top: all
def-Functions — they define, what the program can do. - Below the
if __name__ == "__main__":-Block: the specific order of calls — what should be done this time.
Variables are created in the main block, the functions are called with these variables and the returns are in new Variables saved.
Example 2.1 — Function above, call below
Loading Python…Tests
Module 3 — Calls, return values and variables
The recurring pattern in the main block.
Model
In the main block always applies:
- Inputs into variables first save (e.g. B.
name = "Mia"). - Function with these variables call.
- Return in a new variable caught
(e.g. B.
text = hallo(name)). - Only then
print()call.
This means that intermediate results can be used later and the code remains clear.
TipIn the exercises
With most exercises you read the input values in the main block input() (as introduced in the “Variables” module). The test feeds these values automatic
and compare your output character by character - only output what is required.
With two exercises (part 4 and part 6) you set the values against it yourself
in the main block — without input(). The focus there is on defining the function, and the test calls your function directly with multiple values.
Example 3.1 — input, call, return
Loading Python…Tests
Multiple return values
A function can be included return a, b return multiple values at once. On the call side they are distributed across two variables — this is called
Tupel unpacking.
Example 3.2 — two returns on two variables
Loading Python…Tests
Module 4 — Why the check at all?
What happens without it? if __name__ == "__main__"?
The problem
Do you write function calls and print() simple direct into the file (without main block), they will also when importing executed. That's almost never what you want — you just wanted to use the feature.
With the main block it is clear: “These lines only run if I start the file directly”.
Example 4.1 — direct execution with main block
Loading Python…Tests
TipRule of thumb
The following applies throughout this exercisesseries:
Define function above, in the main block the values provide (per input() read
or set yourself), the function call and the result output. So you don't have to read everything in - sometimes you just set the values yourself.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 6.1.1. Executing modules as scripts: docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts
- Python Library Reference —
__main__— Top-level code environment: docs.python.org/3/library/__main__.html
Tutorials & Manuals
- Real Python — Defining Main Functions in Python: realpython.com/python-main-function
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Section on modules and scripts.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — Chapter on modules.