#47 Python Tutorial for Beginners | Special Variable __name__ part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is Devin ready in the last video we have talked about a very special variable called as name now in that video we have discussed about the importance of name and how do we use it with Maine but again it created some deal of confusion so let's clear it here's if we don't have any doubts in that particular video you don't need to watch this video you can simply skip it and watch the next video in case if you're not filling is with that video this is where I will try to remove your confusions so did you find that let's go for another example here so let's say I'm working on a project and of course when you work on a project you will be grading some modules right so in this case I have two different models we have demo and then we have calc I'm now working only on demo okay I'm not focusing on calc as of now let's focus on demo here now inside demo I want to accomplish some task right and the way you do that is by defining some functions now in this I will define a function let's say different it's we'll say def and it is fun one okay this is my first function and in this function I will do certain tasks I will do some variables have been assigning some values to it and then we will perform some operation on that and at the let me point from one one that's my first function and let me define one more function here the way you do that is by just coming down here and paste and I will define this function name as fine too and I will say this one too okay so imagine this function has a lot of statements now if I try to execute this file if I say run demo of course you will not get any output because we are not calling them right so the ultimately when you define a function you need to also call them the way you do that is by saying fun one and then at you will say fine - okay these are the two statements we are printing here so it means on this code and you can see we are getting fine one and fun to the twisters normally when you define functions normally when you work on a project if this is your stand alone application or stand alone code of this is the only file you are going to write in your project normally what we do is we define everything in a function because statements should be a part of a function so even this function calling should be done inside another function we'll say this is def mean now this is the standard position of which we all follow because if you if you are coming from other programming background let's say C C++ Java we do that okay so main is a starting point of execution so generally we define the function name as main and of course main will not be calling itself right example if I said on demo you can see we are not eating any output because of course you need to call main as well so at the end you will call mean and it will work so you are calling one function main and main will take care how to execute all the statements and then you can call them right this is how generally we make a software okay so we have a main function from the main function you will call all other functions so this perfectly makes sense let's go back to calc okay now let's say I'm defining a calculator here and then in the calculus but let me define some function I will say def some and this function does something of course it will add two numbers and I will say result one is okay something like that okay so I'm defining this function and let me also define another function which is later subtraction okay you can define this function fully if you want but as of now let me just keep it simple so it's a sum and we have sub in fact it should be a Dwight it makes more sense so we have ad and we have sub of course to call this function as well I need to say def and in this def I will say mean of course to call them intermediate the main function and from this mean I will call ad and from this mean I will call sub and at the end we have to call main as well otherwise we cannot call them so I shown this code you can see we got result one is result to a so this is how we define farce right so we got two different modules we got demo and we got gal gand they are working perfectly now you want to use calc in this so when I say function one function one want to use ad so when I say form from one I also want to call ad function from here so let's I want to reuse the code right so I want to reuse this ad function in this demo that's what we do that's why we do find functions now in order to reuse that function what 50 do is we have to say import calc or maybe you can say from calc import ad so now we are importing that particular function which is ad right and you can see we are writing ad here so will this work of course let's try let's run this demo so I'm not running calc now I am running demo right so the outputs I'm expecting is from 1 and from 2 and of course we are calling ad so it should print result 1 but that's this is what I'm expecting I am NOT expecting ezel to because I'm not running calc I'm running demo and in demo we are not using gel to the moment I run this can you see that we are also getting result too and we are getting result one as well this is weird right we don't want this the reason we are using this demo is we want to print from one we want to print from function two and we also want to print result one because we are calling it we are not calling substrate is printing result to its because the moment you import a library if you the moment you import a module it will execute all the statements and in that you can see we have main it is calling main right and main calls all the functions we don't want that right so we have to say hey I want to call main only when I am executing this particular file as a standalone program so I want to call main only when I call this as a standalone program I don't want to call main when I'm running it from another file so here I'm just concerned about Adam not concerned about main so in that case because if I print a statement here I will say in calc mean and if I go back to demo they're not cutting main from here and you can see this still says in calc main why you need to call a main of calc that's the question right because we are running demo this is my first code I am using calc as a module in that case what you will do is you will call main only when I am running calc as a code so you will say if now how do you know that this is the code I'm running so whenever you would on a code and whenever you execute a code there's a variable called as name which will hold the name so if this is the code which I'm running at the start the name variable will have main as a value so let me repeat if this is the file which I'm running this name variable will have main as a value otherwise this name variable will have the module name example let me print inside add let me also print I will say result one is and let me print the name variable let's see what happens so I'm printing a name variable okay let me go back to let me just run this calc first I will say right click and run you can see it says result one is main okay so that means this code is running itself right this is your standalone code the moment you use calc as a module in some other code and I fit on this you can see it says one result one is calc okay it's because the name now is printing the module name so that's important that's the importance of this and you can also see when you run this code we are not getting results due because now you are saying if the name is main then only called main otherwise other code will use this as a module I hope you are getting my point here so I tried my best here so let me know in the comment section if you still have any questions I will try to you know maybe because the another code in future videos will be using this a lot maybe it will clear your confusion there itself so that's it everyone I hope you enjoyed this age let me know in the comment section that's it bye bye
Info
Channel: Telusko
Views: 146,370
Rating: 4.9464979 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: 7hjgRn-vfVQ
Channel Id: undefined
Length: 7min 8sec (428 seconds)
Published: Mon Aug 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.