Python Tutorial 31 --- "If __name__==__main__ usage & necessity"
Python Tutorial 31 --- "If __name__==__main__ usage & necessity" Using if __name__ == “__main__” statement is one such concept that may be difficult to grasp for beginners, but once learned, it is very helpful and used quite often afterward. However, it may seem confusing at times. This article aims to provide an understanding of the behavior of the statement and further discusses how to use it. As discussed earlier, a module is just a file containing a python code with a .py extension and can be imported to other files. That's where the keyword __name__ comes in. Let’s understand __name__ first before moving onto if __name__ == “__main__. What is __name__? “A __name__ is a built-in variable that returns us the name of the module being used.” In simple words, by using __name__ , we can check whether our module is being imported or run directly. If we run it in the same module that it is created in, then it will print “main” onto the screen; otherwise, i...