What does if __name__ == “__main__”: do in Python?

DWQA QuestionsCategory: PythonWhat does if __name__ == “__main__”: do in Python?
Editor Staff asked 2 months ago

What does if __name__ == “__main__”: do in Python?

1 Answers
Editor Staff answered 2 months ago

Whenever the Python interpreter reads a source file, it does two things:

  • it sets a few special variables like __name__, and then
  • it executes all of the code found in the file.