Bird
Raised Fist0

What value does the __name__ variable hold when a Python script is imported as a module?

easy🧠 Conceptual Q1 of Q15
Python - Modules and Code Organization
What value does the __name__ variable hold when a Python script is imported as a module?
A"__main__"
BThe module's name as a string
CThe module's filename as a string
DAn empty string
Step-by-Step Solution
Solution:
  1. Step 1: Understand __name__ when importing

    When a Python file is imported, __name__ is set to the module's name, not "__main__".
  2. Step 2: Differentiate from running directly

    If run directly, __name__ is "__main__"; if imported, it is the module name.
  3. Final Answer:

    The module's name as a string -> Option B
  4. Quick Check:

    __name__ when imported = module name [OK]
Quick Trick: Imported modules have __name__ as their module name [OK]
Common Mistakes:
MISTAKES
  • Confusing __name__ value when running vs importing
  • Assuming __name__ is always '__main__'
  • Thinking __name__ is the filename with extension

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes