if _name_ == __main__: (this script can be imported OR run standalone)
Functions and classes in this module can be reused without the main block of code executing
Good practice (code is modular, helps readability, leaves no global variables, avoids unintended execution)
Ex. library = Import library for functionality. When running library directly, display a help page.
--------- script1.py ---------
This file can run standalone or be imported
def favorite_food(food):
print(f"Your favorite food is {food}")
def main():
print("This is script1")
favorite_food("pizza")
print("Goodbye!")
if _name_ == '__main__':
main()
--------- script2.py ---------
This file should run only standalone
from script1 import *
def favorite_drink(drink):
print(f"Your favorite drink is {drink}")
print("This is script2")
favorite_food("sushi")
favorite_drink("coffee")
print('Goodbye!')
On this page of the site you can watch the video online if __name__ == '__main__' for Python beginners 📥 with a duration of hours minute second in good quality, which was uploaded by the user Bro Code 27 May 2024, share the link with friends and acquaintances, this video has already been watched 42,750 times on youtube and it was liked by 1 thousand viewers. Enjoy your viewing!