14. if __name__ == "__main__"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we are going to talk about very interesting topic if you have looked at Python programs you might have noticed this line and this is very common in any Python program let's see what this is it is saying if underscore underscore name equal to underscore underscore main let's first check out what is underscore underscore name if you have Idol open let me open Idol first we will see what is the value of underscore underscore name when I do this it is telling me the value of this particular variable is mean so this is a predefined variable in Python which is which gets set to mean if you run here for example if you run a dad or py directly then this will be set as mean so let's just run it you see it is now calculating the area okay so this thing is used as kind of like an entry point for any Python program now if you have done any coding in C or Java the entry point in those programming languages is main function as shown here so if you recall if you are in C or Java this is the function from where your program execution begins that's why it is called an entry point similarly in Python this thing is often used as an entry point inside the program okay now when does the value of underscore underscore name is something other than me well if you have another file called caller dot py where you are importing area modules and you're importing area meaning you are importing all the code in area dot py file which is here and I am saying I am in color dot P Y and you are calculating the area okay now if you are in here in area I'm just going to print the value of the name okay so underscore underscore name let's see what is the value of underscore underscore name when we call it from caller dot py okay so I'm saying I am in Colorado right let me add one more line here saying that I am in area dot py okay and first I will execute area dot pure in Colorado py and we will see the difference of this print statement okay now this time I am going to execute it in a little different way usually I run it from py jam editor itself but today I'm going to show you how you can run any Python file using Windows command prompt okay I have Windows command prompt open here and I am in the directory where I have area dot Pui and caller dot P right okay now if I want to execute area dot py directly then this is how I will execute it I will say Python area dot P Y so python is my interpreter and I am executing area or py when I say this I get I am in area dot P Y and the value of underscore underscore name is underscore underscore mean okay let's see what happens if you X you could call our dot P Y when you do that it is saying that I am in color dot P Y but the underscore underscore name value is now area so if you compare that with here it begins execution of this program and that's why I printed I am in Colorado Y here and from here it went into another file which is area so it went in here and when you started executing calculate area the value of underscore underscore name was area so the summary here is when you execute Python area dot py directly the value of underscore underscore name is main and when you execute it from some other file the value of that variable is the name of that module here we are treating area or as a module okay so that was all about underscore underscore name thanks for watching
Info
Channel: codebasics
Views: 9,244
Rating: 4.84375 out of 5
Keywords: python tutorial, python for beginners, learn python, learn python programming, coding tutorial, python beginner tutorial, python programming tutorial, python tutorial for beginners, python coding tutorials, python programming for beginners, tutorial python, tutorial python 3.5, tutorial python 3, python tutorial using pycharm, if __name__ == __main__, __name__ python, __main__ python, python __name__, python
Id: 2AKJzc-cSj8
Channel Id: undefined
Length: 4min 24sec (264 seconds)
Published: Tue May 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.