Bird
Raised Fist0

Consider two files:

medium📝 Predict Output Q13 of Q15
Python - Modules and Code Organization
Consider two files:

# file1.py print('Loading file1') value = 10
# file2.py import file1 import file1 print(file1.value)

What is the output when running file2.py?
ALoading file1 Loading file1 10
BLoading file1 10
C10 10
DError: module imported twice
Step-by-Step Solution
Solution:
  1. Step 1: Understand module import behavior

    Python runs the module code only once, even if imported multiple times.
  2. Step 2: Trace the output

    On first import, 'Loading file1' prints. Second import does nothing. Then prints value 10.
  3. Final Answer:

    Loading file1 10 -> Option B
  4. Quick Check:

    Module code runs once, reused later = B [OK]
Quick Trick: Module code runs once, even if imported multiple times [OK]
Common Mistakes:
MISTAKES
  • Expecting module code to run twice
  • Thinking repeated imports cause errors
  • Confusing print output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes