Hey, in today's code experiments, you will learn some tricks about modules.
That will give you a better understanding of how Python works.
The experiment we want to run is pretty straightforward.
I want you to go to functions that P where we have defined our functions.
And below the functions somewhere down here, just print something like Hello from.
Functions.
So this has to be outside of the function definitions.
You see, it's an indented.
Then I'd like you to go to Maine.
That PI where we import functions that model and run Maine, that pie.
Then in the command line here in the output, what you'll see is helo from functions.
So with this experiment, what I wanted to show you is that when you import a module from another module
and then you run the module where that module is imported.
So when you run main that p y in this script is executed when that script is imported, So the module
is executed at this point in here.
To illustrate it with another example, I'll stop this and go back to functions and introduce an error
here.
Instead of printing out the string, I'll print out something like x.
X is not defined, so this will cause an error.
So now I want to go back to Main that p y or run again.
And this is what we get so of error.
First started.
At this line, line two in main p y.
So that is this line in here.
So this line actually tries to execute functions that p one.
So it tries to execute that.
And then if we trace the error, it says that in line 16 in file functions that p y line 16 is this
one.
We have a name error X is not defined.
So why did I introduce this error?
Well, that was another way to demonstrate that when.
An import operation happens.
It executes functions that P or whatever file you are importing.
This is important to know because.
Sometimes you may want that certain code is only executed, for example.
Hello.
You might want that.
This code is only executed when this script is executed directly.
As in.
Run functions.
So we get Halo here, right?
And so maybe you don't want to execute that halo when you run main that pie, but still you get that
executed.
So how do you avoid executing that?
Line.
When you import, there is a way for to do that, actually.
That is to put this line into a conditional block.
But first of all, why would you want to execute this only when you imposed that function?
Well, maybe you just want to.
Print out.
Get to dos.
Right.
You want to test the function output, so you get that, but you don't want to see that.
When you run Maine.
But again, Maine, that pie will also return that output of the function called Get to deuce.
So we only want to to see that when we execute this code directly, maybe we are testing these functions
out.
So we just want to, to work on these functions that p y.
So the solution, as I told you, is to have a conditional block.
And you say if underscore, underscore, name, underscore, underscore is equal.
So double assignment operator is equal to quotes, open and close.
Underscore, underscore, main, underscore, underscore.
And then you put here a column and then these should be indented under that.
Let me make a break line.
So that's the if conditional.
Now if I run the script functions that p y again I get that executed and that as well as you see the
output in here and there.
But if I execute main that P now you see that.
These two lines were not executed.
We don't have that code in here.
These are executed.
So the functions are defined when we import functions.
And these functions definitions are available here.
So everything that is outside that if conditional.
Such as this print function is executed.
If I execute main that we will see that we get that output because it's outside of that if conditional
block.
Now what is this?
If conditional block, what does it do actually?
Well, we have a variable here.
This is a variable.
But it is a variable which is defined hideously by Python and its value.
And I'll show you what value this name variable has.
So let me print that here.
So print name and I'll execute this script functions.
That's p y and that is the value of name.
This is a string.
So if you check the type.
Of the value of the variable name, you'll see that it's a string and its value is.
Maine.
So that is a string.
But its value is main only when we execute functions that p directly run functions.
If you execute execute main that p y you'll see that the value of name is functions.
So we get that printed out from that line.
So to sum it up, when you run Maine, that's p Y and Maine does P runs functions that p y through that
import statement.
Then the value of name is the name of that file functions in this case.
But when you run functions that p directly.
The value of that variable is underscore, underscore, main, underscore, underscore.
And that allows us to have this condition here.
So if name is equal to that string.
I mean, that means somebody is running this script directly, and that's somebody which is us.
That somebody wants these lines to be executed.
Otherwise, if I'm being imported, the file says, then I will not execute this because my name is
not Main is something else.
So that is the distinction we can make to control what we execute when the file is run directly and
what we execute when the file is run indirectly through this import statement.
This can have practical uses in certain scenarios, especially when you build web applications.
So in that case, you might want to use that to have different outputs when you test the code and so
on.
So that's something you should know and I hope it makes sense.
You'll see this if name is equal to main in other people's code, and this is actually one of the most
asked questions on the internet about Python.
People ask what is if name is equal to main?
I hope this makes sense to you and you don't have the same question later on.
Thanks a lot.
See you later.