In this post, we will see what is the __name__
variable in Python, when is it defined and how it can be used.
Those who have worked in Python might be well aware of the above line of code. But do you know the significance of the variable __name__
?
Do you know how code is executed once it encounters the above statement? If this is something you have always wondered, you will get an answer to your doubts here.
There is no main method which can be known as the starting point in the python code. So how does the interpreter execute our python code?
Indentation is the answer.
The code which begins at level 0, i.e from the far end of the line (which is considered to be level 0) is where the execution starts.
There are multiple levels of indentation in python for all the statements (function definition, if condition, for loop, and so on). When it is time for the code to be executed, the lines of code at indentation 0 are executed first. But right before executing the code, a few special variables are defined. Among these special variables, __name__
is one. The variable __name__
is a built-in variable that helps determine the name of the current module (the one which the user is currently using).
When the main program is the source file that is currently being executed, the python interpreter makes sure to provide the __name__
variable with the value __main__. Else, when a code file/script let's say A.py is being imported from a different module (let's say B), then the value of the __name__
variable would be set to B, indicating the module from which the file has been sourced.
Due to this nature of __name__
variable, it can be used to check if the current python program which is running is the main program that is running on its own or it has been imported from a different module and is being run.
Time for an example!
Before executing the code, create two python files named file_one.py and file_two.py.
The file_one.py will have the below contents:
print("File1 __name__ = %s" %__name__)
if __name__ == "__main__":
print("File1 is being run directly")
else:
print("File1 is being imported")
The file_two.py will have the below contents:
import file_one
print("File2 __name__ = %s" %__name__)
if __name__ == "__main__":
print("File2 is being run directly")
else:
print("File2 is being imported")
Save both these files and run them one by one. Run the file_one.py first and then file_two.py.
Output: (when file_one is executed)
File1 __name__ = __main__
File1 is being run directly
Output: (when file_two is executed)
File1 __name__ = file_one
File1 is being imported
File2 __name__ = __main__
File2 is being run directly
Since file_one runs directly (without importing any other module or file), the python interpreter marks the special variable __name__
as __main__ value. On the other hand, when file_two is executed, it has imported file_one, which means the file_one is not the main program, the file_two is the main program. Hence the __name__
variable of the file_one is set to the name of the Python script, i.e file_one. Hence the output.
Conclusion:
In this post, we understood the significance of the special variable named __name__
in python.