Add THIS To Your Python Scripts! if __name__ == "__main__"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone have you ever seen IFD under name is equal to Dunder Main and a python script and wondered what exactly what its purpose is if you have this video should help you out in explaining what it is and how it could be useful to implement into your own Python scripts so as you can see we're in script 1. pi and from the script I want to print the Der method so the Der method will give us a list of attributes associated with this script so we'll see we have a double underscore annotations all these Dunder variables and one of the variables is the dunder name variable and the dunder name variable does have a value associated with it so in this print statement Let's do an F string so we'll do f with our quotation marks and then we'll do script ones under name is equal to and then we'll put the dunder name here and we can save this and print and script once under name is equal to Dunder main so that's because we're running it from script one so you'll see if we go to script two and then we want to import script one when we import script one it's going to run everything in script one whenever whenever we do the import so we can run this and you'll see that script one's under name has changed to script one as opposed to Dunder main that it was just a second ago that's because we're running it from script 2. okay so let's uh give an example here of how this fact can be useful so let's say in script one we have a function called do something and and do something uh let's just say that this does a bunch of code so we'll just simulate that and we'll do a print statement and saying running one million lines of code okay and then in this script one we'll call the do something function and then over in script two let's import that do something so we'll do from to get rid of this import from script 1 Import do something okay in script two we've imported do something now at some point in script to say we want to actually use the do something function so when we run this it runs it twice so the reason it's running it twice is because it's running it in this import first from script one because it's going to run everything in script one and the script one has a call for do something so back to script two and then when we actually want to use do something in script two it runs it again so that's why here in script one we can do this if under name it's equal to Dunder main so that this do something call in script one only gets called if we're running directly from script1.pi and not when we're importing script one into another script so now we can run this and it only runs it once and that's all there is to it thanks for watching and make sure to drop a like if you found this helpful
Info
Channel: b001
Views: 165,271
Rating: undefined out of 5
Keywords: python, programming, tech, software, engineering, program, technology, coding, code, coder, programmer, engineer, software engineer, computer, computer programming, tips, codelife, computers, __name__, __main__, __name__ == __main__ __name__ == '__main__', __name__ == __main__, if __name__ == __main__ if __name__ == '__main__', if __name__ == __main__
Id: o4XveLyI6YU
Channel Id: undefined
Length: 3min 27sec (207 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.