If name equals main, then we run our programs.
But what does it actually mean?
Here's the simplified take.
If you've used Python
for quite some time, you've probably noticed this type of syntax.
In many cases,
we include it without understanding what it means or why we even need it.
Which is probably not the best practice.
So let's quickly explore it from a beginners friendly perspective.
So first of all, what exactly is name?
Well, name is a special variable
we use when we interact with modules.
It allows us to make a distinction between the modules
we import and the modules we do not import.
Now, so far I was using the term module to describe packages
and libraries such as Pandas and Pygame and so on.
However, this is not the only definition of a module.
A module is a self-contained piece of code.
It is logically separable from other pieces of code.
It is so independent that we can copy a module from one program.
We can then re-use it in another program.
Or we can even execute it all on its own
without any other code surrounding it.
So if that's the case,
not only libraries are modules, but also functions and classes.
And most of all, any python file you have ever
created is also considered a module.
So if we take it back to the name variable,
we can see that it allows us to make a distinction between a
the modules that represent libraries or external functions and classes
which we are able to import into our code
and be the module that represents
the current Python file we are executing in our console.
The file into which we import
those functions and classes and libraries.
Now the best way to explain this is with a code example.
So if you're not sure what I mean so far, you will in just a few seconds.
So let's say we have a Python file named my program RPI,
and it contains a single line of code that just prints.
This is a module.
Now, when we run this file in our console
by typing Python, my program that p y as soon as we hit enter,
we are dealing with something called top level code
as in the entire content of my program is considered top level,
which basically represents the entry point into our application.
What I've previously referred to as the current Python file we are executing.
However, what if we need to import some kind of a library?
So let's say we need pandas in order to create a dataframe.
Well, in that case, the import statement as well as the print statement
are still considered top level code.
However, the dataframe is not considered top level because we have borrowed
a class from a module which we have never specified in our console.
I mean, there is no pandas in Python in my program, the API,
and that's the exact distinction that the name variable helps us solve.
It uniquely identifies the top level module
and separates it from the rest of the modules.
And actually, that's exactly where Main joins the party.
So unlike Name, which is a variable, Main is actually a value.
It is a string.
Main represents the name of the top
level environment in which top level code lives.
So by checking if name equals mean, we are actually checking
if the code we are executing is top level or not.
So for example, if we add an additional print statement
into our file and we pass the global variable of name into it,
our console will return main perfect.
However, if we tried to access the name of pandas, we are no longer
dealing with a global variable but with an attribute of pandas.
And in that case, our console will return pandas instead.
Huh? Now, what if we import pandas as.
Do you think this will affect our name attribute?
Well, let's check and no pandas remains.
Pandas, regardless of the as keyword.
We will see shortly why?
Now, what happens in cases where our import statement is very specific?
What if from datetime we would like to import the timezone plus?
Well, in that case, the name of timezone is timezone.
However, datetime does not have a name at all
unless we specified within an additional import statement.
And then the name of datetime is, of course, datetime.
So the rule of thumb is we strip away the dot by extension
from any python library which we can find in our operating system.
And yes, date time dot P is an actual file
which we can view and we can modify as long as we have Python installed.
Now, once we strip away the extension, we use the name of the file
as the name of the module.
And that's exactly why the as keyword
didn't have any effect on our pandas import.
Now, if we'd like to fetch a specific class from within this file.
So for example, the date class or the timezone class,
we simply use the name of the class as the name of the module and voila.
This basically explains our times example.
So perfect.
Now we know exactly what name and main means, but
why do we need to check if they match before we run our programs?
Let's have a look at an example.
So let's say we have a python file named Import Me
and it contains a function named Call Me which simply prints a short message.
Now, at the very end of this file, we also make a call to the
call me function because, well, it politely asked us to, so we might as well.
Now, when we run this file in our console with Python, import me dot
why we get our print statement and everything works as expected.
However, what if we add another file to the mix?
So let's say that in addition to import me doc
p we have run me dot py.
And the only purpose of this new file is to import
the call me function from the import me module.
There is no other need in this file and there is no other code involved.
Now a very interesting thing happens when we execute this file in our console.
We type python Iran, me dot B, why we hit enter and boom
we can see the output of call me in our console once again,
even though we have never made an official call to this function.
Run me that p y simply includes an import statement. Huh.
So it seems that simply by importing a function
from a module, we also exit mentally execute this module.
So the main purpose of if name equals main
is to prevent this automatic execution from happening.
Now, if we add this condition inside import meet dot by right
above our function call, we can then safely execute, run, meet dot api
without worrying about this extra print statement just popping out of the blue.
So we are basically restricting the behavior of call me
only to situations in which import me
is the top level module in cases in which it's the imported module.
Our call to call me will never manifest
and that's exactly why if name equals mean is so important.
Now, thank you so much for watching.
If you found this tutorial helpful, please
share it with the world and don't forget to give it a huge thumbs up.
If you'd like to see more videos of this kind,
you can always subscribe to my channel and turn on the notification bell
if you have anything to say, please leave me a comment below
with any questions and any requests for future tutorials.
If you'd like to be extra social, you can always join our Discord Community
where you can chat with many, many talented developers.
I'm including the link in the description of force and yeah,
I'll see you very soon in another awesome tutorial in a meanwhile, maybe I.