MOCKSTACKS
EN
Questions And Answers

More Tutorials









If __name__==__main__ usage & necessity


“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, if it is being used elsewhere, then it will print the name of its module or file it is created in.

To fully understand what __name__ is and how it is used, let us go through an example.
#tutmain1.py
print("__name__ in tutmain1.py is set to"+__name__)

Output

__name__ in tutmain1.py is set to __main__


Let us create a new file tutmain2.py in the same directory as tutmain1.py

In this new file, let us import tutmain1.py so that we can examine the __name__ variable in tutmain2.py and let us also print the __name__ variable in tutmain2.py
#tutmain2.py
import tutmain1

print("__name in tutmain2.py is set to"+__name__)

Output

#tutmain1.py
print("__name__ in tutmain1.py is set to"+__name__)


Let us now move further to “if __name__ == “__main__”. Working with Python files, when we import one file to another, along with the functions and variables, we also import all the print statements and other such data that we do not require. In such cases, we insert all the data of the module that we do not want others to import into the main, and thus it can only be executed by the file containing the main only.

Now we may have a certain confusion about “main”, let us clear it out first. The main is a point of the program from where the program starts its execution. Every program has its own main function. The main function can only be executed when it is being run in the same program. If the file is being imported, then it is no longer the main function because the file that is importing it has its own “main” function.
if__name__=="__main__":
    #Logic Statement

What are the Advantages of using if __name__ == “__main__” statement?


Following are the advantages of using if __name__ == “__main__” statement:

Using the main in our file, we can restrict some data from exporting to other files when imported.
We can restrict the unnecessary data, thus making the output cleaner and more readable.
We can choose what others may import or what they may not while using our module.


To summarise the concepts discussed in this tutorial, Modules in Python has a special attribute called __name__. The value of the __name__ attribute is set to __main__ when the module is run as the main program. Otherwise, the value of __name__ is set to the name of the module. The if __name__ == “__main__” block prevents the certain code from being run when the module is imported.

Example


def printhar(string):
    return f"Ye string harry ko de de thakur {string}"

def add(num1, num2):
    return num1 + num2 + 5


print("aand the name is", __name__)

if __name__ == '__main__':
    print(printhar("Harry1"))
    o = add(4, 6)
    print(o)
  


Conclusion

In this page (written and validated by ) you learned about Python If name main usage & necessity . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Join Function.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.