Python Tutorial - 15. if __name__ == "__main__"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to core basics coding tutorial 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 mean 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 main 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 dot py directly then this will be set as me 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 eat the entry point in those programming languages is main function as shown here 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 collar dot py where you are importing area model so you're importing area meaning you are importing all the poor in area dot py file which is here and I'm saying I am in collar dot dy and you're 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 on this Berlusconi let's see what is the value of anaconda school name when we call it from collar dot py okay so I am saying I am in collar dot P right let me add one more line here saying that I am in Illya 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 charm editor itself but today I'm going to show you how you can learn 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 P UI and caller dot TV okay now if I if I want to execute our area dot V Y directly then this is how I will execute it I will say Python ke r dot P Y so Python is my interpreter and I am executing Ariel py when I say this I get I am in area dot P Y and the value of underscore underscore name is underscore underscore main okay let's see what happens if you execute caller dot py when you do that it is saying that I am in caller 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 we execute it from some other file the value of that variable is the name of that module here we are treating area or py as a module okay so that was all about underscore underscore name thanks for watching
Info
Channel: codebasics
Views: 62,566
Rating: 4.557447 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__
Id: Huz6bS0uLm4
Channel Id: undefined
Length: 5min 4sec (304 seconds)
Published: Sat Feb 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.