__name__ in Python

As we know that Python does not have a main() function like C and C++. When the interpreter runs the program, code at level 0 indentation(very first line) starts executing. Before executing the code, the interpreter defines a special variable i.e. __name__.

If you are not familiar with __name__ don’t worry this post is for you. Follow the tutorial to understand the role of __name__ in Python.

Also read: Packing and unpacking arguments in Python

Note: __name__ variable is not available in the Python version below 3.x. 

__name__ in Python

In  Python, __name__ contains the name of the current module. If the same module is executing then the __name__ variable contains the “__main__” otherwise it contains the name of the module imported.

Suppose we have a Python program file name “codespeedy.py”. When we run the file “codespeedy.py” the value of __name__  will be  “__main__”. Another file with “tech.py”  is made and if we import “codespeedy.py” in “tech.py” the value of __name__ will be module name i.e. “codespeedy”.

For better understanding, let’s see an example. As described above create the first file with the name codespeedy.py.

def display(name):
    print(name)

print("__name__ value=", __name__)
print("Displaying Name:")
if __name__ == '__main__':
    display("Vimal Pandey")

Output:

__name__ value= __main__
Displaying Name:
Vimal Pandey

Now create the second file tech.py  and import the first file codespeedy.py in it.

import codespeedy
codespeedy.display("Python Programming")

Output:

__name__ value= codespeedy
Displaying Name:
Python Programming

When the tech.py is executed then the value of __name__  changed to codespeedy from __main__.
That’s why  if__ name__ ==”__main__”  is used to prevent the ceratin lines of code from being imported into another program.

 

Another explanation of __name__ in Python

In Python, we don’t have any main() function. Hence the execution of the code starts from the statement with 0 level indentation. But before the execution of the code, the interpreter defines a special variable __name__. Though it’s value depends on how the script is executed. When we run a program file, the __name__  variable is assigned the value __main__.

But if we import a script or a file, then the __name__ variable is assigned with the name of that particular file. We usually import a file when we want to access the functions present in some other file by importing the file as a module in the current file.

The __name__ variable decides which file to run. Let’s understand the above process using example code.

def main():
   print('__name__ variable value is ',__name__)
if __name__ == '__main__':
   main()

Output:

__name__ variable value is __main__

When we run the above code, initially the interpreter assigns the value ‘__main__’ to the variable __name__. Hence the ‘if’ condition gets satisfied and the program control goes to the main function and the print statement gets executed.

If we import another file within this file, then the code will work in the following manner. Consider a file ‘first.py’.

#first.py
if __name__ == "__main__": 
    print "first is being run directly."
else: 
    print "first has been imported."

Output:

first is being run directly.

When the above code is run the __name__ variable is assigned the value ‘__main__’ and as the ‘if’ condition is satisfied, we get the above output. Now consider another file, ‘second.py’.

#second.py
import first 

if __name__ == "__main__": 
    print "second is running directly."
else: 
    print "second is being imported."

Output:

first is being imported.
second is being run directly.

In the above file, we have imported the file, ‘first’ as a module. Thus when the code runs, the interpreter searches for the file, first.py and runs that file. Now as this file is imported the __name__ variable in this file is set to the name of the file i.e first and the else statement is printed.
Then the interpreter returns back to the file, ‘second.py’.

Please comment below if you find anything incorrect. If you have any other doubts related to this topic or any other topic comment below your problem. We will definitely help you with an easy solution.

Leave a Reply

Your email address will not be published. Required fields are marked *