Every single Python programmer faces a common problem at the beginning. It is the Python special attribute name. It is a simple thing for regular python programmers but a nightmare in the case of beginners. So today am here to tell you what exactly is this Python Special attribute __name__.
Python Special Attribute “__name__”
def Important_function(): print "I love visiting PythonLovers.net" Important_function()
That is a simple program which is first just defining a function and then calling it. When we run this Python Program, we will get output as the line –
I love visiting PythonLovers.net
When we import this module to another script like:
When importing it will call the function “Important_function()” with it. To avoid this, you have to comment out the function call from the bottom of the code. And then you’ll have to remember whether or not you’ve commented out your test function call. That increases the complexity of the program which it may be possible that you will forget.
A Simple Solution-
The Python Special Attribute name __name__ points to the namespace wherever the Python Interpreter happens to be at the moment. Inside the imported module it happens to be the name of that module. But inside a primary module, the value of the name will be the “__main__” which means you are running everything from its “__main__”. So if we modify our code a little –
if "__name__" == "__main__": Important_function()
Now we have provided a check before executing. If we run this, our code will only execute if we are running it as a primary module.
Why Main Check?
By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.
Let’s Have Another Look
If it is not clear to you from above example, I will give another one. Now create two scripts, first “A.py” and other “B.py”. Fill this code in “A.py” –
#A.py import B
And this code in B.py –
#B.py print "String 1 : Hello World from %s !!" % __name__ if "__name__"=="__main__": print "String 2 : This is Hello from %s !!" % __name__
Now, Running this will get this output-
$ python a.py String 1 : Hello World from b!
As you can see, the value of Python Special Character name ( __name__ ) is set to the name of the module printing the String 1 because it is not running the program is primary module.
A Final Touch –
Now to give a final touch so that you can understand it correctly. Execute the following code –
#!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module'
It will provide the Following output –
$ python using_name.py This program is being run by itself $ python >>> import using_name I am being imported from another module >>>
Every Python module has it’s
__name__ defined, and if this is,
'__main__' it implies that the module is being run standalone by the user, and we can do corresponding appropriate actions.
So, this is what it is all about. I may have taken some code examples from Internet just to make it easy for you. If you still have any query about this article, please do comment below or mail me. Till then, Happy Coding.