+ 2
if __name__ == “__main__”
I know what it does. __name__ returns __main__ if it is run by the python interpreter. If it is imported as a module then __name__ becomes the file name which is not main What I dont get is when i would be needing to use this. I see this a lot in videos and websites but it seems to be overused unless im not understanding when we should be using it. Also, what is a practical application of this, is it something that i should be using often? Why would we not want some parts of the program to not run?
3 odpowiedzi
+ 2
Please try and search just so that we can avoid duplicates but this is from 2 years ago so I can see how it was missed. See this question from another:
https://www.sololearn.com/Discuss/2823102/?ref=app
Also, this video gives a good "why" reason which is what you're looking for:
https://youtu.be/o4XveLyI6YU
Also, see this from FCC with some examples of "when": https://www.freecodecamp.org/news/if-name-main-JUMP_LINK__&&__python__&&__JUMP_LINK-example/
Another source for stating how it's good for testing:
https://note.nkmk.me/en/python-if-name-main/
0
Мурод