if __name__ == '__main__' for Python beginners 📥
ฝัง
- เผยแพร่เมื่อ 28 ก.ย. 2024
- 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!')
*You can also run a script by right clicking within that script and selecting 'Run'. I forgot about that shortcut while filming this video.*
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!')
Hello my best developer and youtuber wassup man,will Laravel videos come?
If bro code only has 1 follower...that's me
If bro code has millions of followers... I'm one of them
If bro code has no followers... I'm dead.
As long as you follow him, it doesn’t matter if you’re dead, since it saves 👍
Nah... I'd be dead before subscribing
:)
if ___name___ == "__main__":
print("ok")
else:
delete system32
That’s wrong, it has to be 2 underscores (__name__)
@@SxitxmaNxB yt italized the _ for absolutely no reason
all me python skill came from this guy!
extremely well made videos!
Hey bro! Could you do springboot tutorials. Please
can you please do spring boot series all aboput java fullstack?
Up! Nice topic bro
+1
Bro i start your c++ 6 hours course, i follow all instructions but why my ming64 bin folder empty but your was not empty, also g++ --version command got error, i noticed ucrt64 bin folder have those files that were in your ming64 bin folder, so i give ucrt64 bin folder path that worked when i type g++ --version command,.also got first program output in vsc, when i give path ming64 bin when that time i got error in vsc when i output, i wanna ask is this ok giving ucrt64 bin folder path? becuase Ai gemini says maybe this worked but thats not good,pls reply😔
What happened if I moved the imported file to other folder ?
1:35 how do i do that in vscode?
Sir please continue and complete the react course
すし Gud
Sir please continue and complete the react course
Thank you bro❤
THis is the first time I actually understood why what this function used
Can you please make a tutorial on Golang, i really wanna learn it from you!