Python's if __name__ == "__main__" Explained

Ever wonder what Python's "if name == main" construct does?
This Python tutorial will quickly explain what the if name == main condition does when added to your code.
The quick explanation?
Any time you want a single-point of entry for your Python apps, just put the code that kicks off your app in this block:
if _name_ == '__main__':
Then, when you run your Python app as a standalone application, the code in the if __name__=="__main__" block runs.
This is a smart approach if you want either run or import Python code as a library or a component.
It's similar to the void main() syntax of Java. C and C++ has similar constructs.
So there you go, that's the answer to the age old question of what does Python's if __name__=="__main__": do.
⏲️ KEY MOMENTS ⏲️
------------------------------------------
00:00 - Python Tutorial Intro
00:21 - Python Libraries
00:33 - Top Level Code
00:46 - if name == main
01:00 - Python Components
01:11 - if name == main example
02:30 - Python vs Java
03:09 - Scrum for Python!
03:20 - Is it Over?

Пікірлер: 48

  • @hitechpanchal
    @hitechpanchal3 ай бұрын

    I understood it the first time, But watching you say if underscore underscore name underscore underscore is equal to colon underscore underscore main underscore underscore colon. brought a smile , so i watched it 5 more times: Thank You for the explanation. I was stuck with this for one of my CS50P assignment. And Your explanation cleared it all. Thank You once again 🌷🌷🌷🌷🌷

  • @scrumtuous

    @scrumtuous

    3 ай бұрын

    If I can make people smile while casting some shade on Python, then everything is right in the world. ❤

  • @DarylStark
    @DarylStark9 ай бұрын

    Made me laugh out loud 😂😂

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    Are you saying Python is a joke? ❤ Glad I made you smile.

  • @DarylStark

    @DarylStark

    9 ай бұрын

    @@scrumtuous oh, definitely not. I like Python a lot. Just the way you keep rolling your eyes and keep complaining is absolutely amazing!

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    @@DarylStark I love all my Python dev friends. I know it's a great language. But y'all have grown into tall poppies and there needs to be someone who'll put y'all in your place from time to time! ❤🤣❤

  • @DarylStark

    @DarylStark

    9 ай бұрын

    @@scrumtuous and you're good at doing that! Love it!

  • @Skibbi198
    @Skibbi19810 ай бұрын

    public static void main string args

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    That was 28 years ago. It's changed. You don't have to do that anymore. But yes, if you compare Python to Java in 1996, it's a much closer race for Python. Python still loses though. 🤷‍♀

  • @farzadmf
    @farzadmf10 ай бұрын

    I think it's much easier to pronounce "__" as "dunder" (short for double-underscore) 🙂

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    It's not as good of a troll though. 🤣

  • @farzadmf

    @farzadmf

    10 ай бұрын

    @@scrumtuous so true! 😆

  • @viveknaskar
    @viveknaskar7 ай бұрын

    Well explained, Darcy! Concise yet detailed. 🙌

  • @scrumtuous

    @scrumtuous

    7 ай бұрын

    Thank you so much!

  • @natesofamerica
    @natesofamerica22 күн бұрын

    I'm confused about what you mean by "top level code". I don't see Java as being more elegant here.

  • @scrumtuous

    @scrumtuous

    22 күн бұрын

    Just code in a file, not inside any methods. Just 'top level code.' In this sense, Java is far more elegant, as top level executable code isn't allowed, and no code ever runs simply on import. There are many ways Java is far superior to Python. This is just one of the many security related ones.

  • @guimoncao
    @guimoncao9 ай бұрын

    Everyone in this channel loves java

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    Who doesn't love Java???

  • @Fudmottin
    @Fudmottin10 ай бұрын

    Are we done? How many people do you have in the studio with you? Is it more than __?

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    'The Studio?' You think I only have one studio?

  • @Fudmottin

    @Fudmottin

    10 ай бұрын

    @@scrumtuous Don't deflect. I'm sure each of your mansions has its own studio.

  • @scrumtuous

    @scrumtuous

    8 ай бұрын

    @@Fudmottin I prefer the term "PERSONsions."

  • @Fudmottin

    @Fudmottin

    8 ай бұрын

    @@scrumtuous LOL!

  • @JackalBruit
    @JackalBruit9 ай бұрын

    hmm i *may* have to jazz up """if underscore underscore name underscore underscore equals equals quote underscore underscore main underscore underscore quote colon""" into a lil diddy haha

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    Bohan already did. Seriously.

  • @personalaccount1515
    @personalaccount151510 ай бұрын

    First

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    you rock!

  • @personalaccount1515

    @personalaccount1515

    10 ай бұрын

    @@scrumtuous yes, but not like you, you are the best, TY

  • @pintopaulopt
    @pintopaulopt17 күн бұрын

    So, it sounded to me that Python has a security issue and is safe only on the goodwill of a developer writing it? By the way I will never for __main:__ 😅

  • @scrumtuous

    @scrumtuous

    17 күн бұрын

    All developers are good people, so it's a safe bet. Thanks for watching!

  • @sreekeshiyer
    @sreekeshiyer10 ай бұрын

    Imagine reading the boilerplate code in Java like that

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    There is no boilerplate code in modern Java.

  • @brandoncole1227
    @brandoncole122710 ай бұрын

    Great video! How come you don't have 200,000 views on this!

  • @scrumtuous

    @scrumtuous

    10 ай бұрын

    Nobody loves me. 😥

  • @sixtooxford7242
    @sixtooxford7242Ай бұрын

    The python if name = main thing always confused me.

  • @JackalBruit
    @JackalBruit9 ай бұрын

    if ur looking for an editor .. im cheap haha

  • @JackalBruit
    @JackalBruit9 ай бұрын

    0:30 in .. gee .. i would *hate* to hear her __bash__ my lang of choice haha

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    I wasn't bashing anything. I think I did a great job of promoting Python and explaining it clearly!!! underscore underscore...

  • @chuckynorris616
    @chuckynorris6168 ай бұрын

    If you tell me you're still using Java in 2023 I would just tell you this "The future is now old man"🤣

  • @Iseeuandwatch79
    @Iseeuandwatch797 ай бұрын

    😅 we are done. 😅

  • @scrumtuous

    @scrumtuous

    7 ай бұрын

    Thought that was kinda funny so I left it in.🤣

  • @RangerRick2024
    @RangerRick20243 ай бұрын

    Cool video an you are Amazing superrrrrr cute

  • @vikashg1775
    @vikashg17759 ай бұрын

    All I hear is _ _ _ _ _ 🤣

  • @scrumtuous

    @scrumtuous

    9 ай бұрын

    That's all you were meant to hear! ❤

  • @marianofreddi
    @marianofreddi7 ай бұрын

    Aaaaand another video talking how good Java is. 😂😂

  • @scrumtuous

    @scrumtuous

    7 ай бұрын

    There's still not enough of them!

  • @ShahanKabiri
    @ShahanKabiri13 күн бұрын

    Are you a AI generated Tool....100% Accurate Face....