Bird
Raised Fist0

You have two files:

hard🚀 Application Q8 of Q15
Python - Modules and Code Organization
You have two files:
# main.py
import helper
helper.run()
print('Main:', __name__)

# helper.py
def run():
    print('Helper:', __name__)

if __name__ == '__main__':
    run()

What is the output when running python main.py?
AHelper: __main__ Main: main
BHelper: __main__ Main: __main__
CHelper: helper Main: main
DHelper: helper Main: __main__
Step-by-Step Solution
Solution:
  1. Step 1: Value of __name__ in imported module

    When helper is imported, its __name__ is "helper".
  2. Step 2: Value of __name__ in main.py

    When running main.py directly, its __name__ is "__main__".
  3. Step 3: Output lines

    Importing helper executes its top-level code (defines run()) but skips the if __name__ == '__main__': block since __name__ is 'helper'. Then helper.run() is explicitly called from main.py, printing 'Helper: helper'. Finally, main.py prints 'Main: __main__'.
  4. Final Answer:

    Helper: helper Main: __main__ -> Option D
  5. Quick Check:

    Imported modules have __name__ as module name [OK]
Quick Trick: Imported modules have __name__ as module name, not '__main__' [OK]
Common Mistakes:
MISTAKES
  • Assuming helper.py runs its main block on import
  • Confusing __name__ values in main and imported modules
  • Expecting helper.run() to run automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes