You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

7.3 Top-level code

2 min readjune 24, 2024

When importing in Python, runs automatically. This can set up initial states but may cause unintended side effects. Understanding this behavior is crucial for managing module execution and avoiding unexpected consequences.

To prevent unintended execution, use the

###if___name___==_'[__main__](https://www.fiveableKeyTerm:__main__)':_0###
conditional block. This separates code for from direct execution, allowing modules to be both importable and executable. The
[__name__](https://www.fiveableKeyTerm:__name__)
variable plays a key role in controlling module behavior.

Module Behavior and Top-Level Code

Code execution during import

Top images from around the web for Code execution during import
Top images from around the web for Code execution during import
  • Python executes all top-level code (statements outside functions or classes) when a module is imported
  • Allows module to set up initial state (initialize variables, define constants, establish connections)
  • Be aware of code running during import to understand module's behavior and side effects
    • Module might modify global state or perform resource-intensive tasks (file I/O, network requests)
    • Unintended consequences if not carefully managed (performance impact, unexpected behavior)
  • Top-level code execution contributes to the module's and

Prevention of unintended execution

  • Use conditional block based on
    __name__
    variable to control code execution when importing
  • __name__
    holds the name of current module
    • Set to
      '__main__'
      when module run directly as main program
    • Set to module name when imported
  • Code inside
    if __name__ == '__main__':
    block only runs when module executed directly, not imported
    if __name__ == '__main__':
        # Code here runs only when module is main program
        main()
    
  • Separates code for import from code for direct execution
    • Prevents unintended side effects during import (user interaction, resource-intensive tasks)
    • Allows module to be both importable and executable (library usage vs. standalone program)
  • This serves as the for

Role of name variable

  • __name__
    is a built-in Python variable holding the name of current module
    • Set to
      '__main__'
      when module run directly
    • Set to module name when imported
  • Checking
    __name__
    value controls module behavior based on usage context
    • Different code paths for standalone program vs. library usage
  • Common use cases:
    1. Executing code only when run directly (running tests, command-line interface)
    2. Preventing code execution during import (user interaction, resource-intensive tasks)
    3. Conditionally importing dependencies or configuring module based on usage context
  • Leverages
    __name__
    to create modules that are both importable and executable
    • Flexibility in how module can be used (library or standalone program)
    • Enables modular and reusable code design
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary