Jul-11-2019, 06:43 AM
This line is used to distinguish execution behavior when the file is imported as a module, or is executed as a script. When it is executed as a script builtin variable
Let we have
__name__
is set to __main__
, when the file is imported as a module __name__
is set to filename
. Let we have
myfile.py
with that construction. When you call this file e.g. python myfile.py
the code under if-statement will be executed. If you import the file as a module, e.g. put somewhere import myfile
, the code under if-statement wouldn't be executed.