Pages

Saturday, January 9, 2016

Python: __name__ == “__main__”

As we all know that python does provide built-in class attributes that provide specific functionality while being used with the python interpreter. The available built in class attributes are

__dict__: Dictionary containing the class's namespace.
__doc__: Class documentation string or none, if undefined.
__name__: Class name.
__module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

The main important thing to learn in that above one is __name__ attribute. So a __name__ is a global variable that is exists in all namespace. This can be taught as the module name. So when you run the below code, we see the __name__ points to the __main__ variable.

>>> print "first Module Name: {}".format(__name__)
first Module Name: __main__

Now lets write a file first_module.py as,

[djas999@vx181d imp]$ cat first_module.py

print "First Module name: {}".format(__name__)

if __name__ == "__main__":
    pass

[djas999@vx181d imp]$ cat second_module.py
print "Second Module name: {}".format(__name__)

Now when we run these we see,

[djas999@vx181d imp]$ python first_module.py
First Module name: __main__

[djas999@vx181d imp]$ python second_module.py
First Module name: first_module
Second Module name: __main__

The name in the first_module point to __main__ where as when we imported the first module the __name__ points to the name of the module. The python interpreter will make sure to check the source code whether it was running directly or being imported.

One of the reason for this is python does not have  a main method unlike many other high level language so most of the time we write a main() method which we think as a starting point and hence python starts the main() method when the __name__ points to the __main__ in code.

The reason why the __name__ behaves differently is that when we have code that we want to execute only on some cases. When python imports a module ,it will execute the code in the module but when  we make the code to go to a main() method then it cannot run the code in main() method as the __name__ will point to the module name rather than the __main__ method.

Now add a main method to the first_module as

[djas999@vx181d imp]$ cat first_module.py
def main():
    print "First Module name: {}".format(__name__)

if __name__ == "__main__":
    main()

Now when we execute the code we see

[djas999@vx181d imp]$ python first_module.py
First Module name: __main__

[djas999@vx181d imp]$ python second_module.py
Second Module name: __main__

We can see even though we had imported the first_module in second_module we were not able to run the code in main() method as __name__ was pointing to the module name. we can make a call first_module.main() which will then invoke the code in the main() method.

Hope this helps, more to Come.

No comments :

Post a Comment