Built-ins & Standard Library · Question 13 · Medium
A file tools.py ends with a block under if __name__ == "__main__":. When does that block run?
- Only when tools.py is run directly, not when it is imported by another file
- Only when tools.py is imported by another file
- Every time, whether tools.py is run or imported
- 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
- A deque gets an appendleft, a pop and a rotate(1). What does it hold at the end?
- max is called on a dictionary of city temperatures, once plainly and once with…
- zip is used with the * operator to split a list of pairs. What does this code…
- itertools is used to count the 2-item combinations and the 2-item permutations…
- sorted is given a key that returns a tuple, to rank students by marks (high to…
- itertools.groupby is applied directly to a string in which one letter appears…
- Booleans are used in an isinstance check, in addition and inside sum. What…
- A program imports the same module helper from two different files. How many…
- A defaultdict(int) is updated for one key, read for a second key and tested…
- all, any and max are each given an empty list, with a default supplied to max…
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