Welcome back.
In order to finalize our understanding of modules, we have to talk about an important concept, which
is this idea of main.
Or specifically underscore underscore main.
Because as you start developing in your career, you're going to start to see a lot of code like this.
Now, this is one of the most common lines you're going to see when working with Python.
And in this video, I'm going to explain what it does.
Because it is one of the most confusing topics, especially for beginners.
So for now I'm going to comment out and do a few things.
We have our main PI file here.
And I want to do a few things.
First, I'm going to open up our shopping cart, which is right here.
And at the top over here, I'm just going to print because right now our file isn't doing anything.
I'm going to say print, underscore, underscore name like this and I'm going to do the same for the
utility dot py.
So at the top, utility pie.
Now if I run Main here, let's click run here.
I get these two things printed.
Now, why is that?
Well, when I run a python file, usually we just run one file, right?
The main file that we're working on.
In our case, our main file just happens to be called main dot py.
So when I click run, the interpreter goes line by line through here.
The first thing it does is import the utility module.
And import these two files.
So what it's going to do is go to the utility file and run this and automatically each file that we
run or each module.
Is given a name when our case, it's the name of the module utility.
So when you import something in Python, it finds the module, it runs a code.
So it adds this code to memory because we're going to have to use multiply, divide it max and then
goes on to the next line.
In our case, it's this one where we import again from the shopping cart.
So it's going to say, hey, the name of this is shopping dot more shopping dot shopping cart because
that's the module name.
And then finally it's going to go through here because by this point by line for these files have been
run and they're in memory now.
Now the interpreter has access to divide, multiply and all these functions that we've imported.
Now, here's my question.
What if I do here or at the end do print?
Underscore.
Underscore name on this main file.
If I run here.
You see that?
I get underscore, underscore, main.
Is that what you expected?
What if I change the name of this file?
Let's say refactor.
And say rename.
Yeah, that's okay.
We want to rename it and we'll rename it to let's say Test Pi.
So now this is called test pi.
What do you think the underscore name of this is?
If I run here.
I still get underscore underscore me.
So maybe this is starting to make sense now when you see the code.
This if underscore underscore name equals to underscore, underscore, mean, underscore.
So these are Dundas.
Then do something.
And this the name mane is given specifically to the file that we run.
Because remember, when we run a file, when I do play or when I do in my terminal python three and
then say test dot py I run that one single file and that one single file imports different modules and
runs them.
That one file that we run gets this default underscore underscore main.
So the reason you might see lines like this in Python is that sometimes we want to make sure that we
run a module only if this is the module or the main module.
So I can say here, print, please run this.
And let's remove this print for now and click Run.
You see that this I get please run this.
But if I copy this code and let's say put it into utility.
Like this and I click run.
You see that?
Please run.
This does not appear in utility only at the end when we're running this test.
Because.
Why?
Well, the name of this file is not Main.
The name, remember, is utility.
So this is a good way for us to only run code of the main file.
So this is a common structure as projects get bigger and bigger.
Maybe we have code that we don't want to run unless it's the main file.
In our case, let's say that we don't want to run any of these.
Unless this is the main file being called.
If I run this now.
You see that?
Oh, and I get an error.
It says line one in module cannot import name multiply from utility because well we aren't able to import
this because we didn't run this piece of code.
So let's revert that back.
Come back here.
And what we can do now is actually wrap this entire thing.
Like this.
So that we make sure that test only runs if well, that only if this is the one being run.
It shouldn't get imported.
It should just be the main file that we run.
Now, remember when we had a class, let's say we had a main file and we created a class, let's say
student.
And this student let's say it doesn't have anything but when we do type of.
Student.
And we do print here.
And I click Run.
Oh, let's instantiate the student.
So let's say student one equals student.
And let's just get the type of the student.
If I click Run.
Do you remember how we have this underscore underscore main dot student?
What it's saying is this class was created.
In the main file that we ran.
If, for example, I copy this, added it to the utility like this.
And now in my main file.
I import utility.
And let's say utility dot sd one and do.
And then print.
What will happen then?
Well, we get class utility dot student.
So whenever we saw that main, all that it was saying is, hey, this was a class that was created in
the main file.
Again, this is one of those things that you're going to see a lot of.
But at the end of the day, all it means is I want to make sure that if this is the file that's being
run, because a file can be either run or it can be imported.
If this is the file that gets run, it's the main file.
Then do something.
So it's a good way to check how our modules are being used, whether imported or ran.
Hopefully that made sense.
In the next video, we're going to talk about something exciting called Built in Modules.
I'll see you in that one.
Bye bye.