0:00
hi everyone my name is jaal today I'm
0:02
going to teach you what if name equals
0:05
main uh is in Python and how it works so
0:09
if name equals main essentially is
0:11
something that you see at the bottom of
0:12
a script telling you if you should
0:14
execute it and what it says is if the
0:17
name equals main then execute uh
0:19
whatever is found inside the python file
0:22
uh but it will only execute if you run
0:25
it directly and not if you import it as
0:28
a module so in order to make that
0:30
clearer uh I'm going to create this main
0:33
module that will essentially just print
0:35
the name here and I will run this to
0:39
make make it clearer so I can do Python
0:41
3 uh main module. pi and if you do this
0:47
you can see that the the name of the
0:51
main If instead I decide that I want to
0:55
import uh main module instead of just
0:59
running so I have this import main
1:02
function that will import the name
1:05
module and essentially it will execute
1:08
this print name so if I do Python 3 in
1:12
this case and I do import main so now
1:16
it's not uh around as the python file
1:19
but as an import module you can see that
1:23
the name of the file the name will
1:26
return is essentially the name of the
1:28
module in which is F found so we can
1:32
Define that it was important imported in
1:36
this case and not important in that case
1:39
so if we look at uh the main module uh
1:43
we can now show how if name equals main
1:46
works so because if I do if
1:54
equals main so in that case if it's run
2:04
something uh and in this case uh it will
2:07
only run if I uh import it
2:12
directly so we can do something uh we
2:15
can define a function in this case we
2:17
can say Okay Main we can create that
2:20
main function and say uh print that it
2:25
was executed and instead of saying do
2:27
something we can say main here
2:30
so essentially what this will do is if I
2:32
run it directly it will print the name
2:35
here and it will print the main function
2:38
was executed and I can also put a na
2:41
statement to decide what I want it to do
2:44
if it doesn't so in order to make it
2:46
clearer I will print the main function
2:49
did not execute so if I save this file I
2:53
can show that I can do python 3E main
2:57
module to Pi and you can see that main
3:00
function was executed when the name was
3:02
a CL qu Main and if I import it you can
3:06
see that the main function did not
3:09
execute so that's basically what it is
3:12
so it's not as complex as it looks like
3:16
um but that's that's all it is so make
3:20
sure that you subscribe to my YouTube
3:22
channel and stay tuned for the next
3:24
video tutorial see you later