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!')

ความคิดเห็น • 23

  • @BroCodez
    @BroCodez  4 หลายเดือนก่อน +5

    *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!')

    • @kamranalizade98
      @kamranalizade98 4 หลายเดือนก่อน

      Hello my best developer and youtuber wassup man,will Laravel videos come?

  • @Zero-Dev64
    @Zero-Dev64 4 หลายเดือนก่อน +25

    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.

    • @SxitxmaNxB
      @SxitxmaNxB 4 หลายเดือนก่อน +1

      As long as you follow him, it doesn’t matter if you’re dead, since it saves 👍

    • @BaldiReycaster
      @BaldiReycaster 4 หลายเดือนก่อน +1

      Nah... I'd be dead before subscribing

  • @BaldiReycaster
    @BaldiReycaster 4 หลายเดือนก่อน +1

    :)

  • @_EnderTv
    @_EnderTv 4 หลายเดือนก่อน +4

    if ___name___ == "__main__":
    print("ok")
    else:
    delete system32

    • @SxitxmaNxB
      @SxitxmaNxB 4 หลายเดือนก่อน

      That’s wrong, it has to be 2 underscores (__name__)

    • @_EnderTv
      @_EnderTv 4 หลายเดือนก่อน

      @@SxitxmaNxB yt italized the _ for absolutely no reason

  • @olivergrim7634
    @olivergrim7634 4 หลายเดือนก่อน +3

    all me python skill came from this guy!
    extremely well made videos!

  • @vibingowsic2338
    @vibingowsic2338 4 หลายเดือนก่อน +2

    Hey bro! Could you do springboot tutorials. Please

  • @mintz347
    @mintz347 4 หลายเดือนก่อน +6

    can you please do spring boot series all aboput java fullstack?

  • @muhammadislam7836
    @muhammadislam7836 4 หลายเดือนก่อน +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😔

  • @sammyay-man2754
    @sammyay-man2754 หลายเดือนก่อน

    What happened if I moved the imported file to other folder ?

  • @c-ronos
    @c-ronos 4 หลายเดือนก่อน +1

    1:35 how do i do that in vscode?

  • @technicalswag3925
    @technicalswag3925 3 หลายเดือนก่อน

    Sir please continue and complete the react course

  • @SmortMen
    @SmortMen หลายเดือนก่อน

    すし Gud

  • @technicalswag3925
    @technicalswag3925 3 หลายเดือนก่อน

    Sir please continue and complete the react course

  • @TamilCoder.
    @TamilCoder. 4 หลายเดือนก่อน

    Thank you bro❤

  • @Little-bird-told-me
    @Little-bird-told-me 4 หลายเดือนก่อน

    THis is the first time I actually understood why what this function used

  • @Kartik-malviya
    @Kartik-malviya 4 หลายเดือนก่อน

    Can you please make a tutorial on Golang, i really wanna learn it from you!