- 1
if __name__ == '__main__':
I can't understand it.Please someone explain it step by step.
2 Answers
+ 4
Here __main__ is just a string that is used to check if the current module/script is running on its own or not. In the __name__ variable, the double underscores on both sides of name is for indicating the python interpreter to recognize it as a special/reserved keyword.
Some resources to understand better:
1 - What does the if __name__ == â__main__â: do?
https://www.geeksforgeeks.org/what-does-the-if-__name__-__main__-do/
2 - Python if __name__ == __main__ Explained with Code Examples
https://www.freecodecamp.org/news/if-name-main-python-example/
3 - __main__ â Top-level script environment
https://docs.python.org/3/library/__main__.html
4 - https://www.studytonight.com/python/_name_-as-main-method-in-python
+ 1
that's a conditional statement wich will run the next indented block only if the script is running at first one (not if imported as module)...