The Main Guard
Talk it through out loud with your AI coach.
The problem
- easy
- Pro
You import a helper function from a colleague's script, and as soon as the import runs, the whole script starts processing files. Explain why, and what the if name equals main check at the bottom of many Python files is for.
What a strong answer covers
- What happens to a module's top-level code when it's imported.
- How a file can tell whether it was run directly or imported.
- Keeping a file usable both as a script and as a library.
- Another situation where the check really matters.
Common mistake: Treating the check as boilerplate without being able to say what the special name holds in each case.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud