As a python beginner in 2024, I say thank you for this tutorial. There are so many wack articles out there trying to explain 'if __name__ == "__main__":'. I rewound the video a few times but I understand it now.
it was a very good explanation - especially when it ran both codes after the import - you can see what is clearly happening and why you need a 'guard', as he says...I am soo happy that i understand this now and I can carry on with my python learning : )
So what is the difference between two following code if I just execute them as script alone? def greeting(): print('Hello World!') greeting() and def greeting(): print('Hello World!') if __name__ == "__main__": greeting() Thanks
No difference - if all you're running is that code at once (i.e. from one python file or manually entered at the command line). The file that's initially run and pulls modules/libraries into it is decreed as __main__. so each version of your code will be __main__.
i think you add it to the start of the programme where it needs to begin - so after you have set up your functions or imported any modules add it after those - i hope i am correct : )
I have one question if the developer doesnot use if __name__=='__main__' statement while making my_module.py then either you add if __name__=='__main__' in main_script.py or not add , it will not work as you said. So developer must have to use the above statement so that we can use module and got expected resutl. So, again question is that if developer has already used the above statement in my_module.py then why should we use that statement again in main_script.py. As we know using it in main_script.py doesnot make any sense. CORRECT ME IF I AM WRONG because your example didnot completely made the sense. ----------------my_module.py------------------- def abc(a,b): return a+b print('hello') ---------------main_script.py-------------- import my_module if __name__=='__main__' : print(test.abc(5,4)) NOTE: I think we actually add that statement in main_script.py because we make our own function before starting if __name__ == '__main__' statement in main_script.py. So I think we do so inorder to use that main_script.py as module in future.
In my case I want code in the module to run when it’s imported, so I don’t use this. Is that valid, or does that mean I’m making a poor design choice somewhere?
great video K0nze!! - by showing us what happens with the two codes running after the import, you made me understand and why you need a 'guard' to block off other lines of codes running in the imported module - well done!!
Get your FREE Python Cheat Sheets: k0nze.gumroad.com
As a python beginner in 2024, I say thank you for this tutorial. There are so many wack articles out there trying to explain 'if __name__ == "__main__":'.
I rewound the video a few times but I understand it now.
I've been struggling with this notion of __name__ == "__main__"
for a long time.
Thanks a lot for a clear and concise explanation !
After watching multiple videos explaining this concept, I finally understand it from you. Thanks!
Only video I understood on this matter. You have the skills of a true teacher. Also, the scenery is lovely. Nice nature background :D
Thank you so much 😀
To the point explanation.... always ignore why it's written in python but now you cleared that. Keep making such high quality content ❤
Awesome video K0nze! Excellent explanation!
Thank you Mo!
before this video i went through 3-4 tutorials to understand this point , it got cleared in your video
great explanation !!
I have watched several videos on this subject. Yours is the very best.
Thank you!
only comprehensive explanation out there!
it was a very good explanation - especially when it ran both codes after the import - you can see what is clearly happening and why you need a 'guard', as he says...I am soo happy that i understand this now and I can carry on with my python learning : )
thanks, i watched other videos on this topic, but your video was the best and i was able to clearly understand the concept you were explaining
Clean, Clear and Crisp. Million thanks for the great video!
🖥Great explanation.
Wow! I finally know! Thanks for the explanation! 🖥
Understood
🖥watched till the end :) haha thanks for the nice explanation!
best explanation ever after lots of videos. GJ bro! thanks 🖥
🖥️ Thanks for the explanation!
thank you!
thank you mate
thank you for the clear explanation!
Thank you
Great video! Thanks 🖥️
🖥Thank's. Now I get it!
I feel silly, I just asked this question on the previous video I watched 🤣 🖥
brilliant
So what is the difference between two following code if I just execute them as script alone?
def greeting():
print('Hello World!')
greeting()
and
def greeting():
print('Hello World!')
if __name__ == "__main__":
greeting()
Thanks
No difference - if all you're running is that code at once (i.e. from one python file or manually entered at the command line). The file that's initially run and pulls modules/libraries into it is decreed as __main__. so each version of your code will be __main__.
So simply it means don't run imported code unless it is called.
Thank you👍
Great explanation. I get it. c💻
Hi, I like to buy your cheat sheets but via Pay Pall. Is this possible?
Great video! How did you format the terminal showing this arrow-like format for your path?
❤❤❤
Keep it up
Why not "dunder"? Instead of "underscore, underscore"?
At least it is faster.
Is there a way to avoid executing my_module code without editing it adding if __name__=="__main__" ?
🖥🖥👍
WHERE do you add it? At the bottom after functions or at the top? (requires indenting all the time)
i think you add it to the start of the programme where it needs to begin - so after you have set up your functions or imported any modules add it after those - i hope i am correct : )
@@kools67 Tq
🖥
good video (computer emoji)
💻
bester mann
STEVEN BRIAN JOYNER
I have one question if the developer doesnot use if __name__=='__main__' statement while making my_module.py then either you add if __name__=='__main__' in main_script.py or not add , it will not work as you said.
So developer must have to use the above statement so that we can use module and got expected resutl.
So, again question is that if developer has already used the above statement in my_module.py then why should we use that statement again in main_script.py. As we know using it in main_script.py doesnot make any sense. CORRECT ME IF I AM WRONG because your example didnot completely made the sense.
----------------my_module.py-------------------
def abc(a,b):
return a+b
print('hello')
---------------main_script.py--------------
import my_module
if __name__=='__main__' :
print(test.abc(5,4))
NOTE: I think we actually add that statement in main_script.py because we make our own function before starting if __name__ == '__main__' statement in main_script.py. So I think we do so inorder to use that main_script.py as module in future.
In my case I want code in the module to run when it’s imported, so I don’t use this.
Is that valid, or does that mean I’m making a poor design choice somewhere?
If your module is not runnable standalone, you don't need to add it.
So basically: code in this block will only run if you run that file directly. explained in 1 sentence.
🖥
💻
great video K0nze!! - by showing us what happens with the two codes running after the import, you made me understand and why you need a 'guard' to block off other lines of codes running in the imported module - well done!!
Thanks! I finally understand this disgrace 😅
Thanks
💻🖥bro u are great
🙃
🖥️
ty. Excelent
🖥 thanks
well explain, thank you🙂🙂
You're welcome 😊
Thanks so much for this video, I spent hours trying to understand the main method from other resources and could not get it until now!
🖥
🖥
🖥
🖥
i can see clearly now... the confusion is gone...ty!
🖥 Wonderful video) Thank you very much !
Straight to the point! Thanks 👍🏽 🖥
thanks man :)