Python Tutorial: if __name__ == '__main__'

preview_player
Показать описание
In this video, we will take a look at a common conditional statement in Python:
if __name__ == '__main__':

This conditional is used to check whether a python module is being run directly or being imported.

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

Dude, it's incredible how simple this concept is when it's explained well. So many other resources had my head spinning about it, but just one 8 minute YouTube video later, and I'm like "oh yeah well obviously."

samiam.
Автор

Short answer: this set up gives you the option to run (or not run) a chunk of code when imported from another python file

blmcmcn
Автор

Subscribed:  

That was by far the best explanation I've found on YouTube regarding this topic.  I hope you continue to make videos.

tyroneslothdrop
Автор

Hi, Corey. If you ever wonder if you're making a difference in the world, then let me assure you that you are. Your videos make learning and using Python possible for those who watch them.


Beautiful work. Thank you very much.

kogcyc
Автор

I have found a way of conceptualizing this line of code in a way that (for me) may be easier to understand ...

Think of any single individual who is part of a family; we will call this person Xavier.
Let's talk about the relative terms we use when relating Xavier to the rest of his family.
His parents consider Xavier to be his SON.
His sister considers Xavier to be her BROTHER.
His second-tier relatives consider Xavier to be their COUSIN.
His grandparents consider him to be their GRANDSON.
Lastly, Xavier considers Xavier to be HIMSELF.

Xavier is still the same person in each of these relative relationships, but the relative term we use for him is different depending on who we are relating to.

So, when we write, "If __name__ == __main__" then we are effectively asking is: "If <relative pronoun> == HIMSELF"
If that code evaluates to TRUE, then we know that the program is running on its own - it is referring to itself.
<relative pronoun> == "HIMSELF"
If, however, that code evaluates to FALSE, then we know that the program is running as a component of another program.
That is, when you run the code "if __name__ == __main__" and it evaluates as FALSE, then it would be **metaphorically** equivalent to the return values being something like the following:
<relative pronoun> == "SON"
<relative pronoun> == "BROTHER"
<relative pronoun> == "COUSIN"
<relative pronoun> == "GRANDSON"
In other words, Xavier is not operating as HIMSELF, he is operating in relation to some other entity (a subcomponent of another program).

EVUTube
Автор

It's hard to believe that such a complicated concept can be explained like this. Cheers Cory, the great!

ashishdeora
Автор

I thought that geneticists are terrible at naming things but coders have their share of confusing nomenclature. Despite that, I got it because of this video! Thanks Corey!

vavilon
Автор

I hope you know that even 6 years after posting your content is still helping beginners like me understand python better than anything else on the internet can! Thank you for your work, it's great! :)

rukhan
Автор

Man, this is the first video where I actually get what this means. You are the GOAT, my friend.

zsszeli
Автор

The absolute clear and concise explanation I've seen on this in 5 years off and on of playing and working with Python. Well done!

brandonhunter
Автор

What everyone else said: best explanation available. Thank you very much.

nigelmtb
Автор

THIS MAKES SO MUCH SENSE. I spent an hour and a half today rewatching the video i learned it from and trying to get a grasp on it. Makes a lotta sense when you explain and show it the way you do. It's sort of like a validation prompt to see which file is being run to avoid duplicates etc.

AliColak
Автор

Haven't encountered an explanation as good as this. Thank you!

The world needs teachers like you.

supermanish
Автор

okay, this person is so nice that he repeated the same thing without sounding repeatative at all. anyway, coming to the point, I think for average, slow, diverted students the video needs to be seen twice or whatever, there's got to be some kind of repeatation; but for focused students he's a great teacher

anindam
Автор

I considered "if __name__ == '__main__'' as some magical conditional check that triggered the main function for me, until I watched this video today. It's clear now. Your explanation was so clear and simple it made so much sense. Thanks.

garydunken
Автор

New to Python, I'm a C# developer, this video was exactly what I needed... just had to find the right one so it would click. THANK YOU!

jenniferf.
Автор

man you are awesome, I started with the first video of Flask series and now I come here for understand the ifmain conditional.
thanks for your youtube channel, it will be for very much help, I am a new subscriber, like in all your videos.
Greetings from México.

humanOSx
Автор

I stuck for DAYS on this, THANK YOU SO MUCH for explaining the topic really clearly.

potterdkk
Автор

Thank you very much - I have read and read on this subject but was getting totally confused. This video explains things so much better. Thank you very much. Your video is very explanatory and well done. :¬)

JoeEvansSound
Автор

I've literally been searching for this for the past two years. So simple yet I haven't seen anyone explain it better!

abu