Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the purpose of the if __name__ == '__main__': guard?

  1. A To run code only when the file is executed directly, not when imported
  2. B To define the program entry point required by Python
  3. C To prevent syntax errors
  4. 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.

All Python MCQs

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as