if __name__ == '__main__' for Python beginners đŸ“„

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 26. 05. 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!')

Komentáƙe • 21

  • @BroCodez
    @BroCodez  Pƙed měsĂ­cem +4

    *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 Pƙed měsĂ­cem

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

  • @Zero-Dev64
    @Zero-Dev64 Pƙed měsĂ­cem +18

    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 Pƙed měsĂ­cem +1

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

    • @BaldiReycaster
      @BaldiReycaster Pƙed měsĂ­cem +1

      Nah... I'd be dead before subscribing

  • @olivergrim7634
    @olivergrim7634 Pƙed měsĂ­cem +2

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

  • @Little-bird-told-me
    @Little-bird-told-me Pƙed 29 dny

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

  • @technicalswag3925
    @technicalswag3925 Pƙed 25 dny

    Sir please continue and complete the react course

  • @TamilCoder.
    @TamilCoder. Pƙed měsĂ­cem

    Thank you bro❀

  • @vibingowsic2338
    @vibingowsic2338 Pƙed měsĂ­cem +1

    Hey bro! Could you do springboot tutorials. Please

  • @mintz347
    @mintz347 Pƙed měsĂ­cem +4

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

  • @BaldiReycaster
    @BaldiReycaster Pƙed měsĂ­cem +1

    :)

  • @Kartik-malviya
    @Kartik-malviya Pƙed měsĂ­cem

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

  • @muhammadislam7836
    @muhammadislam7836 Pƙed měsĂ­cem

    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😔

  • @_EnderTv
    @_EnderTv Pƙed měsĂ­cem +2

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

    • @SxitxmaNxB
      @SxitxmaNxB Pƙed měsĂ­cem

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

    • @_EnderTv
      @_EnderTv Pƙed měsĂ­cem

      @@SxitxmaNxB yt italized the _ for absolutely no reason

  • @evozeus
    @evozeus Pƙed 29 dny

    1:35 how do i do that in vscode?

  • @technicalswag3925
    @technicalswag3925 Pƙed 25 dny

    Sir please continue and complete the react course