Bird
0
0

Why is it recommended to keep __init__.py files minimal and avoid heavy code execution inside them?

hard📝 Conceptual Q10 of 15
Python - Modules and Code Organization
Why is it recommended to keep __init__.py files minimal and avoid heavy code execution inside them?
ABecause __init__.py cannot contain any executable code
BBecause code in __init__.py runs on every import, slowing startup
CBecause Python ignores __init__.py content during import
DBecause __init__.py is only for documentation
Step-by-Step Solution
Solution:
  1. Step 1: Understand when __init__.py runs

    Code inside __init__.py runs every time the package is imported.
  2. Step 2: Impact of heavy code

    Heavy or slow code here delays import and startup of programs using the package.
  3. Final Answer:

    Because code in __init__.py runs on every import, slowing startup -> Option B
  4. Quick Check:

    Keep __init__.py light to avoid slow imports [OK]
Quick Trick: Avoid heavy code in __init__.py to keep imports fast [OK]
Common Mistakes:
  • Thinking __init__.py can't have code
  • Assuming Python ignores __init__.py
  • Using __init__.py only for docs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes