1
00:00:05,550 --> 00:00:11,340
To finish off this section of the course I want to discuss these two ideas of name and then the string

2
00:00:11,410 --> 00:00:17,780
main so often confusing part of Python is when you start looking at larger dot pie scripts that you

3
00:00:17,780 --> 00:00:22,610
haven't written yourself you often see at the very bottom this mysterious line of code that says if

4
00:00:22,980 --> 00:00:28,610
underscore underscore name underscore underscore is equal to the string underscore underscore main underscore

5
00:00:28,610 --> 00:00:32,340
underscore colon and then there's a block of code to be executed.

6
00:00:33,550 --> 00:00:38,890
So sometimes the whole idea behind that line of code is that we're importing from a module.

7
00:00:39,010 --> 00:00:44,440
You would actually like to know whether a modules function is being used as an import or if you're using

8
00:00:44,440 --> 00:00:46,880
the original but py file of that module.

9
00:00:48,020 --> 00:00:50,750
So we're work to do is explore all of this a little bit more.

10
00:00:50,960 --> 00:00:55,280
But make sure to check the full explanatory text file that has a written explanation of everything we're

11
00:00:55,280 --> 00:00:56,410
going to cover here.

12
00:00:56,660 --> 00:01:01,340
Well we're going to do basically is just create two simpled up postscripts and try to show you the idea

13
00:01:01,370 --> 00:01:02,280
behind that.

14
00:01:02,360 --> 00:01:08,430
If name is equal to main block of code that is usually at the end a larger dot PI script files.

15
00:01:08,450 --> 00:01:09,330
Let's get started.

16
00:01:10,370 --> 00:01:15,390
OK let's begin by actually creating two new files will create them at my desktop.

17
00:01:15,470 --> 00:01:21,040
I will say for this first file save as and I'm going to say this as one dot pi.

18
00:01:21,260 --> 00:01:24,240
So this one is one of pi.

19
00:01:24,350 --> 00:01:32,750
Then I'll create a new file and I'm going to save this one as to that pi to that PI again at my desk

20
00:01:33,090 --> 00:01:35,180
will level this one to that pi.

21
00:01:35,500 --> 00:01:37,790
So make sure you have those saved.

22
00:01:38,440 --> 00:01:43,510
So let's think about what actually happens when you call a Python script at your command line.

23
00:01:43,570 --> 00:01:47,960
You go to your command line you type python space the name your script the PI.

24
00:01:47,980 --> 00:01:54,940
So what's actually happening when you call Python my script that pi all the code that's the indentation

25
00:01:54,940 --> 00:01:59,860
level 0 essentially something like this is going to get run.

26
00:01:59,860 --> 00:02:03,870
So that means any code that takes the level 0 is going to get run.

27
00:02:03,910 --> 00:02:09,250
And that would mean that your functions which are automatically usually going to be defined in the notation

28
00:02:09,250 --> 00:02:11,010
a little 0 in your classes.

29
00:02:11,020 --> 00:02:14,670
Also the indentation level 0 are all going to be defined.

30
00:02:14,710 --> 00:02:21,040
So unlike other languages in Python there's no main function that you call somewhere at the top of your

31
00:02:21,040 --> 00:02:23,530
script that gets run automatically.

32
00:02:23,530 --> 00:02:29,500
Instead what happens is just implicitly all the code at the indentation level 0 gets run when you call

33
00:02:29,500 --> 00:02:34,180
the script file and you probably have an intuition of that by now since we've been working through that

34
00:02:34,180 --> 00:02:35,020
strategy.

35
00:02:35,020 --> 00:02:43,720
The entire first half of the Course now in Python there is actually a built in variable called name.

36
00:02:43,720 --> 00:02:48,520
So just like there's built in functions this is a built in variable and it's quite obvious that it's

37
00:02:48,520 --> 00:02:53,010
built in because we have two sets of double underscores in the beginning and in the back of it.

38
00:02:53,020 --> 00:02:57,600
So that would allow you to have to really try to overwrite it by accident.

39
00:02:57,610 --> 00:03:04,690
So what happens is this variable name gets assigned a string the pending on how you're running the actual

40
00:03:04,690 --> 00:03:05,460
script.

41
00:03:05,590 --> 00:03:08,170
And if you run the script directly.

42
00:03:08,170 --> 00:03:14,800
So if I went to the command line and I wrote out Python space one that PI with Python is going to do

43
00:03:14,800 --> 00:03:20,110
it's going to assign this builtin variable called name to be equal to

44
00:03:22,820 --> 00:03:24,310
underscore underscore mean.

45
00:03:24,320 --> 00:03:26,310
So Python does this in the background.

46
00:03:26,480 --> 00:03:28,280
Well it really does this.

47
00:03:28,280 --> 00:03:31,100
It assigns the string to the name variable.

48
00:03:31,130 --> 00:03:37,280
When you run it directly and that allows you to have an IF block to check if they're equal to each other

49
00:03:37,910 --> 00:03:39,530
so often will you do something like this.

50
00:03:39,530 --> 00:03:46,880
You say if name is equal to Main and then you can do something because you know that this particular

51
00:03:47,010 --> 00:03:49,520
pipe file is being run directly.

52
00:03:49,760 --> 00:03:53,930
And if you ever run something directly of Python you know this case happens to be true.

53
00:03:54,230 --> 00:03:59,420
So happens is often people just at the very bottom of their code they'll say if name is equal to Main

54
00:03:59,600 --> 00:04:03,690
and then they say kind of run a bunch of functions that they defined here.

55
00:04:03,740 --> 00:04:09,980
So at top they do a bunch of function definitions so function of one function and two so on and so on.

56
00:04:09,980 --> 00:04:13,820
So you can imagine hundreds of lines of function definitions or classes and then all the way at the

57
00:04:13,820 --> 00:04:19,040
bottom they're actually organized or code on what they actually want to execute and they know that they

58
00:04:19,040 --> 00:04:23,750
want to execute it because they say if this built in variable called name happens to be equal to the

59
00:04:23,750 --> 00:04:28,320
string main then I know I'm running this py file directly.

60
00:04:28,640 --> 00:04:33,680
So we're going to explore this whole idea a little further and to do that we're going to do the following

61
00:04:34,100 --> 00:04:35,550
in one pie.

62
00:04:35,570 --> 00:04:37,410
So let's go back here.

63
00:04:37,490 --> 00:04:38,650
This is one that pi.

64
00:04:38,780 --> 00:04:40,150
I will write out the following code.

65
00:04:40,160 --> 00:04:45,890
I will say DPF create a function and this function is going to be very simple.

66
00:04:45,890 --> 00:04:50,780
It will say func in one PI.

67
00:04:50,840 --> 00:04:57,020
So when you call that function it says func in one that pi just printed that out and then I'm also going

68
00:04:57,020 --> 00:05:00,530
to do here is at an indentation level of 0.

69
00:05:00,530 --> 00:05:09,250
So outside of this function I will say top level in one dot pi.

70
00:05:09,290 --> 00:05:14,790
So now I have a function definition as well as a global print statement top level and one that pi.

71
00:05:14,810 --> 00:05:19,400
Then at the bottom of this I will say if you'll notice that's a blind text that will actually autocomplete

72
00:05:19,400 --> 00:05:22,890
this for you if name is equal to mean.

73
00:05:23,030 --> 00:05:24,430
I'm going to delete this.

74
00:05:24,590 --> 00:05:26,450
So I have an IF block of code here.

75
00:05:27,590 --> 00:05:31,410
So if name is equal to made that I know I'm running this directly.

76
00:05:31,430 --> 00:05:42,150
So I will say one that pi is being run directly and then I'm actually going to lineup in ELSE block

77
00:05:42,150 --> 00:05:43,440
with this if statement.

78
00:05:43,500 --> 00:05:47,060
You don't see this that often usually you see if and then you execute your code.

79
00:05:47,190 --> 00:05:51,890
But just to make it clear how this variable is working will also add in an else there will say.

80
00:05:51,960 --> 00:05:52,840
Else.

81
00:05:52,980 --> 00:06:00,140
Prince one PI has been imported.

82
00:06:00,260 --> 00:06:01,390
So I'm going to save that.

83
00:06:01,520 --> 00:06:03,350
And let's take a look at what happens.

84
00:06:03,350 --> 00:06:04,940
I defined this function.

85
00:06:05,000 --> 00:06:10,790
I print out this statement whenever you call 1 that pi and then depending if one PI is the main script

86
00:06:11,080 --> 00:06:16,970
meaning and calling one PI directly at the command line I will print this out else that means the variable

87
00:06:16,970 --> 00:06:21,920
name was not sent to Maine and I will print this out when that pie has been imported.

88
00:06:21,980 --> 00:06:30,670
So now let's work with our second file to that pie what do here is say import 1 and I will print out

89
00:06:30,760 --> 00:06:34,850
at the very top level.

90
00:06:35,080 --> 00:06:45,550
Let's print out something like top level in two dot pie and since I just imported one let's execute

91
00:06:45,550 --> 00:06:52,140
the function that's from one or say one that funk and here are going to have an if statement will autocomplete

92
00:06:52,150 --> 00:06:56,700
this if name is equal to mean.

93
00:06:56,890 --> 00:06:59,180
I will print two.

94
00:06:59,370 --> 00:07:06,110
The pie is being run directly else.

95
00:07:06,300 --> 00:07:14,450
Prince to the pie has been imported.

96
00:07:14,630 --> 00:07:18,810
So let's save the changes in both of these and then open up our command line.

97
00:07:20,310 --> 00:07:26,640
All right so the first thing we're going to do at our command line is actually run one up PI directly.

98
00:07:26,820 --> 00:07:27,620
So we run this.

99
00:07:27,660 --> 00:07:28,770
And we notice what happens.

100
00:07:28,770 --> 00:07:35,580
We get that print statement top level in one PI and then we see from that if name is equal to mean one

101
00:07:35,580 --> 00:07:37,210
that pi is being run directly.

102
00:07:37,320 --> 00:07:41,670
So we can see here that that built in variable underscore underscore name underscore underscore has

103
00:07:41,670 --> 00:07:43,950
been assigned that main string.

104
00:07:43,950 --> 00:07:49,440
So Paice on the nose and the entire script canel that one that has been called directly.

105
00:07:49,530 --> 00:07:54,270
But now let's check out Python to that pi.

106
00:07:54,270 --> 00:07:58,650
So now when I run this notifier happens I get back toplevel and one that PI.

107
00:07:58,890 --> 00:08:07,450
But now one that PI it's built in variable name has not been set to mean because it's been imported.

108
00:08:07,470 --> 00:08:13,350
So now we get back one that pi has been imported and then I get top level and to that pie funk in one

109
00:08:13,530 --> 00:08:16,620
pie and then to that Pye's being run directly.

110
00:08:16,620 --> 00:08:23,260
So that built in variable actually allows you to see whether something is being imported or run directly.

111
00:08:23,320 --> 00:08:29,130
So that's the idea in concept behind the variable name name and checking in physical to main.

112
00:08:29,140 --> 00:08:34,600
Typically however what you see is people they don't really do it the way we've done here with a check

113
00:08:34,920 --> 00:08:36,770
is something being run directly or not.

114
00:08:36,790 --> 00:08:42,980
Instead if we come back to Sublime Text Editor what they do is instead of having an L statement here

115
00:08:44,530 --> 00:08:50,880
since they just say if name is equal to me this is the same thing as saying run the script right.

116
00:08:50,960 --> 00:08:54,900
Because we already know that if this happens to be the case and someone is running one that PI script

117
00:08:54,900 --> 00:08:55,800
directly.

118
00:08:55,850 --> 00:09:00,650
So what they do is they define a bunch of functions up here or find their classes.

119
00:09:00,680 --> 00:09:03,290
So here's another function pass.

120
00:09:03,460 --> 00:09:08,420
You write some other function function to say and then down here at the bottom what you end up doing

121
00:09:08,420 --> 00:09:14,180
is you just call your function so you'll say OK execute function to then execute function 1 and then

122
00:09:14,180 --> 00:09:19,060
maybe have a little bit of logic here but the main idea is that this is more for code organization.

123
00:09:19,130 --> 00:09:23,330
You have all your functions and classes defined up here and then your logic where you actually execute

124
00:09:23,330 --> 00:09:24,640
things are defined.

125
00:09:24,680 --> 00:09:29,540
Here at the bottom and this is a really common structure you will see for larger that by scripts especially

126
00:09:29,540 --> 00:09:32,870
when you begin to use modules and packages.

127
00:09:32,880 --> 00:09:33,300
All right.

128
00:09:33,320 --> 00:09:35,000
That's the basics of this line.

129
00:09:35,000 --> 00:09:36,670
If name is equal to main.

130
00:09:36,710 --> 00:09:40,210
Definitely check out the explanation that textfile that goes along with this lecture.

131
00:09:40,220 --> 00:09:43,310
And if you have any questions feel free to post the Q&amp;A forums.

132
00:09:43,310 --> 00:09:44,420
I'll see you at the next lecture.
