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
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
3 ай бұрын
If I can make people smile while casting some shade on Python, then everything is right in the world. ❤
Made me laugh out loud 😂😂
@scrumtuous
9 ай бұрын
Are you saying Python is a joke? ❤ Glad I made you smile.
@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
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
9 ай бұрын
@@scrumtuous and you're good at doing that! Love it!
public static void main string args
@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. 🤷♀
I think it's much easier to pronounce "__" as "dunder" (short for double-underscore) 🙂
@scrumtuous
10 ай бұрын
It's not as good of a troll though. 🤣
@farzadmf
10 ай бұрын
@@scrumtuous so true! 😆
Well explained, Darcy! Concise yet detailed. 🙌
@scrumtuous
7 ай бұрын
Thank you so much!
I'm confused about what you mean by "top level code". I don't see Java as being more elegant here.
@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.
Everyone in this channel loves java
@scrumtuous
9 ай бұрын
Who doesn't love Java???
Are we done? How many people do you have in the studio with you? Is it more than __?
@scrumtuous
10 ай бұрын
'The Studio?' You think I only have one studio?
@Fudmottin
10 ай бұрын
@@scrumtuous Don't deflect. I'm sure each of your mansions has its own studio.
@scrumtuous
8 ай бұрын
@@Fudmottin I prefer the term "PERSONsions."
@Fudmottin
8 ай бұрын
@@scrumtuous LOL!
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
9 ай бұрын
Bohan already did. Seriously.
First
@scrumtuous
10 ай бұрын
you rock!
@personalaccount1515
10 ай бұрын
@@scrumtuous yes, but not like you, you are the best, TY
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
17 күн бұрын
All developers are good people, so it's a safe bet. Thanks for watching!
Imagine reading the boilerplate code in Java like that
@scrumtuous
10 ай бұрын
There is no boilerplate code in modern Java.
Great video! How come you don't have 200,000 views on this!
@scrumtuous
10 ай бұрын
Nobody loves me. 😥
The python if name = main thing always confused me.
if ur looking for an editor .. im cheap haha
0:30 in .. gee .. i would *hate* to hear her __bash__ my lang of choice haha
@scrumtuous
9 ай бұрын
I wasn't bashing anything. I think I did a great job of promoting Python and explaining it clearly!!! underscore underscore...
If you tell me you're still using Java in 2023 I would just tell you this "The future is now old man"🤣
😅 we are done. 😅
@scrumtuous
7 ай бұрын
Thought that was kinda funny so I left it in.🤣
Cool video an you are Amazing superrrrrr cute
All I hear is _ _ _ _ _ 🤣
@scrumtuous
9 ай бұрын
That's all you were meant to hear! ❤
Aaaaand another video talking how good Java is. 😂😂
@scrumtuous
7 ай бұрын
There's still not enough of them!
Are you a AI generated Tool....100% Accurate Face....