Understanding if __name__ == “__main__” in Python | Python Main Function with Examples
ฝัง
- เผยแพร่เมื่อ 18 ก.ย. 2024
- 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 ★★★
Python Programming Course ➡️ bit.ly/2vsuMaS ⚫️ bit.ly/2GOaeQB
Java Programming Course ➡️ bit.ly/2GEfQMf ⚫️ bit.ly/2Vvjy4a
Bash Shell Scripting Course ➡️ bit.ly/2DBVF0C ⚫️ bit.ly/2UM06vF
Linux Command Line Tutorials ➡️ bit.ly/2IXuil0 ⚫️ bit.ly/2IXukt8
C Programming Course ➡️ bit.ly/2GQCiD1 ⚫️ bit.ly/2ZGN6ej
C++ Programming Course ➡️ bit.ly/2V4oEVJ ⚫️ bit.ly/2XMvqMs
PHP Programming Course ➡️ bit.ly/2XP71WH ⚫️ bit.ly/2vs3od6
Android Development Course ➡️ bit.ly/2UHih5H ⚫️ bit.ly/2IMhVci
C# Programming Course ➡️ bit.ly/2Vr7HEl ⚫️ bit.ly/2W6RXTU
JavaFx Programming Course ➡️ bit.ly/2XMvZWA ⚫️ bit.ly/2V2CoAi
NodeJs Programming Course ➡️ bit.ly/2GPg7gA ⚫️ bit.ly/2GQYTQ2
Jenkins Course For Developers and DevOps ➡️ bit.ly/2Wd4l4W ⚫️ bit.ly/2J1B1ug
Scala Programming Tutorial Course ➡️ bit.ly/2PysyA4 ⚫️ bit.ly/2PCaVj2
Bootstrap Responsive Web Design Tutorial ➡️ bit.ly/2DFQ2yC ⚫️ bit.ly/2VoJWwH
MongoDB Tutorial Course ➡️ bit.ly/2LaCJfP ⚫️ bit.ly/2WaI7Ap
QT C++ GUI Tutorial For Beginners ➡️ bit.ly/2vwqHSZ
★★★ Online Courses to learn ★★★
Get 2 FREE Months of Unlimited Classes from skillshare - skillshare.eqc...
Data Science - bit.ly/2lD9h5L | bit.ly/2lI8wIl
Machine Learning - bit.ly/2WGGQpb | bit.ly/2GghLXX
Artificial Intelligence - bit.ly/2lYqaYx | bit.ly/2NmaPya
MERN Stack E-Degree Program - bit.ly/2kx2NFe | bit.ly/2lWj4no
DevOps E-degree - bit.ly/2k1PwUQ | bit.ly/2k8Ypfy
Data Analytics with R - bit.ly/2lBKqz8 | bit.ly/2lAjos3
AWS Certification Training - bit.ly/2kmLtTu | bit.ly/2lAkQL1
Projects in Java - bit.ly/2kzn25d | bit.ly/2lBMffs
Machine Learning With TensorFlow - bit.ly/2m1z3AF | bit.ly/2lBMhnA
Angular 8 - Complete Essential Guide - bit.ly/2lYvYRP
Kotlin Android Development Masterclass - bit.ly/2GcblsI
Learn iOS Programming Building Advance Projects - bit.ly/2kyX7ue
★★★ Follow ★★★
My Website - www.codebind.com
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!
Best ever tutorial on name==main
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.
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!
I’ve been struggling to understand this concept with all other videos, and you gave me eureka!!🎉 thank you!
nice explaintion
Very clear explanation
perfect concise explanation for me. Thanks so much. I was very confused before. Now I totally understand
Best explanation
This video: Me: imports 500 libraries into Hello World.py program
Very clear explanation
Thank you
i was wandering here and there looking for precise explanation for this and you explained it very well sir thank you so much
Great explanation of if __name__ == "__main__":
Best explanation. Thank you very much
I have watched a couple of videos on this topic and they didn't do any good except more confusion. But this explanation so clear without going around the bush. Keep us the good work. 👏
Good explanation. Thank you.
Great video and clear explanation. Keep up the good work!
thanks for the video you help me a lot
Very clear explanation. Thank you!
Thanks for this good video.
thank you so much sir!!!!!!!!!!
Amazing explanation
Very good explanation and example! Thank you!
Amazing and concise explanation, sir. Thank you very much!
Perfect example!
best explanation😃
Great video! Really broke it down. Thumbs up! :)
good explanation
Top video, so easily understandable ! Thanks a lot! 👍👍👍
great explanation thanks a lot!
tnx for this turtrial ,finally i understood this
thanks for this
💯💯💯💯💯💯💯💯💯💯💯💯💯💯💯
well explained
Great
Excellent explanation!
Well Done Sir. I am not confused anymore. TY
Now it makes sense! Thanks bro...
🌟Good !👍👆👏😎🌟
Very clear, didnt get the proper idea, while watching other videos in youtube
Very helpful, love you ):D
U made it so clear that I subscribed you :D
Thank man
Thank you!
best one
I am checking again from you, why anyone will in first place write a statement to run the same module which has been defined above. What purpose it would serve in the first place. If have to run the module I will import it and then run the function. First I unnecessarily put code inside my module file to run the code and then put if __name__=“__main__” to prevent it from running. One reason for putting such code is to make .py file more versatile, it can be run directly without importing hence use of less memory and can be imported and run as and when required inside another application. Please enlighten us. Thanks in advance.
thank you...
3 blogs, 2 videos - No use
This 1 video - Enough
Watched 3 videos before this but didn't get it. Thanks for clarifying in such as a simple way.
good job! quite explicit
Awesome explanation.
Beautiful explanation thanq
awesome explanation 👌cant be better 👍
Thanks a lot for making many useful video and playlist tutorial like this.
Really helpful for CS student like me.
:)
My first year lecturer spoke with that same exact ascent 😂
which college?
thx man
does the spawned process also get the same __main__ value for __name__? how does this work with multiprocessing?
What IDE do you use?
Pycharm
@@usama57926 thank you
I understand how it's used but not why there is a use for it. The return statement in a function is syntactically used to exit a function, why would there be additional statements after the return statement? Seems like bad programming, not sure how, when or why there is a use case for this...anybody knows?
Can we use "from mymath import add" ?
I think we can
How result invalid syntax
buyuksun hoca
great explanation, but too much underscores!!! )) its dunder methods
test.py the universal name😂😂😂😂
nice explanation