What is if __name__ == "__main__"?

Like other programming languages, Python too has an execution entry point i.e. main. A module is a file containing Python definitions and statements. Every module in python has a special attribute called __name__ . The value of __name__ attribute is set to '__main__' when module run as main program.

When you execute a Python script , it is treated as the main and its __name__ attribute is set to "__main__" . If you import this script as a module in another script, the __name__ is set to the name of the script/module.

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. Consider the following code for better understanding, it checks if a module is being imported or not.
 (C) 2022    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About