capypad
0 day streak
python / beginner
Snippet

The __name__ == '__main__' Guard

This idiom ensures that code inside the block only runs if the script is executed directly, and not when imported as a module.

snippet.py
python
1
2
3
4
5
def main():
print("Hello World")
 
if __name__ == "__main__":
main()
Breakdown
1
if __name__ == "__main__":
Checks if this specific file is the entry point of the program.