Understanding if __name__ == “__main__” in Python | Python Main Function with Examples

preview_player
Показать описание
In this Python Tutorial for Beginners video I am going to show you the Idea behind using : if __name__ == "__main__" in Python. __name__ is a built in variable in Python. the value of __name__ is set to "__main__" at runtime. Each module has a name, and this name can be used to access instructions in a module. This is especially useful in a given situation: As mentioned before, the main block of a module is executed when it is imported for the first time. But what do we do if we want the block to run only if the module was started as a stand-alone program, but not if it was imported from another module? We can do this by evaluating the attribute __name__ of the module.

#PythonTutorialforBeginners #ProgrammingKnowledge #LearnPython #PythonCourse
★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
Рекомендации по теме
Комментарии
Автор

Grateful that I managed to find this after several videos, managed to get the concept straight away after watching this. Thanks for the crystal explanation and the example.

heaven
Автор

holy shit, I spent like an hour trying to figure out what this means, but every website went into so much details that I got so confused. it was simply that __name__ was = to __main__ when running it directly from the program it was created in at first or __name __ is = to that particular file name it was created in when its imported into another program. Thank you!

erikp
Автор

I’ve been struggling to understand this concept with all other videos, and you gave me eureka!!🎉 thank you!

jojomythe
Автор

perfect concise explanation for me. Thanks so much. I was very confused before. Now I totally understand

elhipster
Автор

Great video and clear explanation. Keep up the good work!

Sam-jgzv
Автор

i was wandering here and there looking for precise explanation for this and you explained it very well sir thank you so much

uditkumar
Автор

Very good explanation and example! Thank you!

riverasanchez
Автор

Amazing and concise explanation, sir. Thank you very much!

FelLoss
Автор

Great explanation of if __name__ == "__main__":

kennld
Автор

Top video, so easily understandable ! Thanks a lot! 👍👍👍

Online-rrpk
Автор

Well Done Sir. I am not confused anymore. TY

hotshotgolfer
Автор

Great video! Really broke it down. Thumbs up! :)

brielleshepard
Автор

tnx for this turtrial, finally i understood this

pouyapargam
Автор

Very clear, didnt get the proper idea, while watching other videos in youtube

keralatalks
Автор

thanks for the video you help me a lot

dayanarodriguezponce
Автор

U made it so clear that I subscribed you :D

nitinmaurya