Practice mode Python

Why is the block if __name__ == “__main__”: commonly used in a Python module?

Correct answer A. So the code inside runs when the file is executed directly but not when it is imported

Explanation

When a file runs as a script, its __name__ is "__main__"; when imported, __name__ is the module name. Guarding script code with this test lets the file be reused as a module without running its tests or command-line logic on import. Python has no required main() entry point.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.