What is the purpose of the if __name__ == '__main__': guard?
- A To run code only when the file is executed directly, not when imported
- B To define the program entry point required by Python
- C To prevent syntax errors
- D To mark the file as executable
Answer
To run code only when the file is executed directly, not when imported
Without it, importing a module would execute its script-level code as a side effect, which also breaks multiprocessing on some platforms.





