Built-ins & Standard Library · Question 13 · Medium

A file tools.py ends with a block under if __name__ == "__main__":. When does that block run?

  1. Only when tools.py is run directly, not when it is imported by another file
  2. Only when tools.py is imported by another file
  3. Every time, whether tools.py is run or imported
  4. Only when tools.py defines a function called main
Answer and explanation

Answer: A. Only when tools.py is run directly, not when it is imported by another file

Explanation: Python sets __name__ to "__main__" in the file that is being run directly. When the file is imported, __name__ is the module name "tools", so the condition is False and the block is skipped.

Previous: zip is used with the * operator to split a list of pairs. What does this code… · Next: itertools is used to count the 2-item combinations and the 2-item permutations…

Practise built-ins & standard library

Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.

More built-ins & standard library questions

More python practice

Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge