Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

if __name__ == "__main__": Use

Betulgg (330 points)
1 4 6
in Programming in Python by (330 points)
When we finally execute our defined function at the end of the code, why we use the statement:

if __name__ == "__main__":

Even we don't write this if statement and simply call our function by our_function(), it is still executed. Then what is the use of this if statement? Can anyone help me with it
324 views

6 Answers

S
Shenok (1590 points)
0 0 10
by (1.6k points)
It's a "boilerplate" code. It protects users from invoking the script when not explicitly required (example: import time execution(if you import a guardless script it wil be executed when a second script import the former ))
James_F (6070 points)
10 14 47
by (6.1k points)
everything that is under the indentation of if __name__ == "__main__": gets executed only if you execute your program directly.
If you use your my_program.py as a library (importing it into another file.py) __name__ would be different from "__main__" and those instruction won't be executed.
Julio Zenelaj (3190 points)
1 8 32
by (3.2k points)
As the professor explained in class, it is useful in cases when you want to use certain functions of your file in other files, while keeping the current file also working as a standalone.

So, if you wanted to use some of the functions of file A on file B, you could use the import statement to import the necessary stuff from A. If you wish to stop here, you won't be able to run anything in A; you'll only be able to call its functions through B. However, if you want A to also run separately, you write the necessary code under if __name__ == "__main__":  .
d
davide.avolio (2170 points)
1 2 10
by (2.2k points)
There are two cases: whether we are working on a single file or multiple files.

Single file
The expression if "_name_" == "__main__" is always true and then it will be executed all the instructions placed in it and outside it. So it's better to not leave instructions not indented, but to control all the execution inside "__name__" == "__main__"

Multiple files
When you have multiple files (where one is importing another), again it's not recommended to leave non-indented instructions, because now it will be executed the instruction of all the files.

By putting instructions inside the "__name__" == "_main__" we can control the execution in a better way. Indeed the expression "__name__" == "__main__" is true only for the file from which the execution started, so it will execute only the instructions placed inside the file you are observing, which surely is what you consider as the main file.
francesco.calzona (5210 points)
5 20 81
by (5.2k points)
Basically, it checks if the name of file where the code is in is "main"; if that is true, it will execute the instructions beneath it.

In our case, as the file name is always main, it always runs the block under this condition; it is useful as you may use this file as an external file where you import some code from, and just by calling it with any other name than "name", it will not execute those instructions (of course this is very useful in the exact way professor Mancini used it: testing. You are more likely to use a function in other files after already have tested it on its own).
yGifoom (2160 points)
0 0 20
by (2.2k points)
If you import a file, the interpreter executes it entirely.

Using that expression explicits that the code that follows it is to be executed only if the file is not imported, in that case only the code before that expression will be executed.