if __name__ == '__main__': | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

if __name__ == '__main__':

I can't understand it.Please someone explain it step by step.

28th Jun 2021, 3:21 AM
Md Nazim Uddin Sagor
Md Nazim Uddin Sagor - avatar
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
28th Jun 2021, 3:28 AM
A͢J
A͢J - avatar
+ 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)...
28th Jun 2021, 3:23 AM
visph
visph - avatar