What is __name__?

QuestionsQuestions8 SkillsProPython Control StructuresSep, 09 2025
099

__name__ is a special built-in variable in Python that represents the name of the current module. It helps to determine the context in which a Python script is being executed.

  • If a Python file is run directly (e.g., python script.py), __name__ is set to "__main__".
  • If the file is imported as a module in another script (e.g., import script), __name__ is set to the name of the module (in this case, "script").

This distinction allows you to write code that can behave differently depending on whether it is run as a standalone program or imported into another module.

0 Comments

no data
Be the first to share your comment!