python

🔶 Python: `if __name__ == '__main__'`

if __name__ == '__main__':
    main()

What it does: Checks if the file is being run directly (not imported).

  • Run python script.py → __name__ is '__main__' → code inside executes
  • Another file does import script → __name__ is 'script' → code inside is SKIPPED

Why it exists: So you can write a module that's both importable (other files use its functions) AND runnable as a standalone script.