1
00:00:02,120 --> 00:00:07,760
Hey, in today's code experiments, you will learn some tricks about modules.

2
00:00:09,920 --> 00:00:13,460
That will give you a better understanding of how Python works.

3
00:00:13,940 --> 00:00:17,720
The experiment we want to run is pretty straightforward.

4
00:00:17,750 --> 00:00:22,850
I want you to go to functions that P where we have defined our functions.

5
00:00:24,830 --> 00:00:32,630
And below the functions somewhere down here, just print something like Hello from.

6
00:00:34,050 --> 00:00:34,950
Functions.

7
00:00:36,240 --> 00:00:39,710
So this has to be outside of the function definitions.

8
00:00:39,720 --> 00:00:43,380
You see, it's an indented.

9
00:00:45,610 --> 00:00:47,260
Then I'd like you to go to Maine.

10
00:00:47,260 --> 00:00:54,940
That PI where we import functions that model and run Maine, that pie.

11
00:00:58,310 --> 00:01:05,900
Then in the command line here in the output, what you'll see is helo from functions.

12
00:01:07,040 --> 00:01:15,200
So with this experiment, what I wanted to show you is that when you import a module from another module

13
00:01:15,290 --> 00:01:19,460
and then you run the module where that module is imported.

14
00:01:21,270 --> 00:01:34,530
So when you run main that p y in this script is executed when that script is imported, So the module

15
00:01:34,530 --> 00:01:37,680
is executed at this point in here.

16
00:01:40,220 --> 00:01:49,340
To illustrate it with another example, I'll stop this and go back to functions and introduce an error

17
00:01:49,340 --> 00:01:49,850
here.

18
00:01:52,050 --> 00:01:56,910
Instead of printing out the string, I'll print out something like x.

19
00:01:56,910 --> 00:02:01,470
X is not defined, so this will cause an error.

20
00:02:02,870 --> 00:02:06,590
So now I want to go back to Main that p y or run again.

21
00:02:08,370 --> 00:02:12,570
And this is what we get so of error.

22
00:02:13,540 --> 00:02:15,070
First started.

23
00:02:15,970 --> 00:02:20,140
At this line, line two in main p y.

24
00:02:20,560 --> 00:02:23,200
So that is this line in here.

25
00:02:26,960 --> 00:02:32,540
So this line actually tries to execute functions that p one.

26
00:02:33,310 --> 00:02:35,080
So it tries to execute that.

27
00:02:35,080 --> 00:02:44,860
And then if we trace the error, it says that in line 16 in file functions that p y line 16 is this

28
00:02:44,860 --> 00:02:45,190
one.

29
00:02:45,190 --> 00:02:48,130
We have a name error X is not defined.

30
00:02:49,520 --> 00:02:52,760
So why did I introduce this error?

31
00:02:52,790 --> 00:02:58,190
Well, that was another way to demonstrate that when.

32
00:02:59,200 --> 00:03:02,650
An import operation happens.

33
00:03:02,920 --> 00:03:07,750
It executes functions that P or whatever file you are importing.

34
00:03:08,800 --> 00:03:10,870
This is important to know because.

35
00:03:11,700 --> 00:03:19,930
Sometimes you may want that certain code is only executed, for example.

36
00:03:19,950 --> 00:03:20,610
Hello.

37
00:03:20,640 --> 00:03:21,600
You might want that.

38
00:03:21,600 --> 00:03:27,030
This code is only executed when this script is executed directly.

39
00:03:27,030 --> 00:03:28,050
As in.

40
00:03:28,990 --> 00:03:30,400
Run functions.

41
00:03:33,340 --> 00:03:35,350
So we get Halo here, right?

42
00:03:35,770 --> 00:03:44,530
And so maybe you don't want to execute that halo when you run main that pie, but still you get that

43
00:03:44,530 --> 00:03:45,130
executed.

44
00:03:45,130 --> 00:03:49,330
So how do you avoid executing that?

45
00:03:50,600 --> 00:03:51,410
Line.

46
00:03:51,410 --> 00:03:56,300
When you import, there is a way for to do that, actually.

47
00:03:57,630 --> 00:04:02,250
That is to put this line into a conditional block.

48
00:04:02,970 --> 00:04:08,820
But first of all, why would you want to execute this only when you imposed that function?

49
00:04:08,850 --> 00:04:11,490
Well, maybe you just want to.

50
00:04:12,730 --> 00:04:13,780
Print out.

51
00:04:14,770 --> 00:04:16,510
Get to dos.

52
00:04:19,250 --> 00:04:19,880
Right.

53
00:04:20,000 --> 00:04:26,120
You want to test the function output, so you get that, but you don't want to see that.

54
00:04:28,350 --> 00:04:30,030
When you run Maine.

55
00:04:30,030 --> 00:04:38,370
But again, Maine, that pie will also return that output of the function called Get to deuce.

56
00:04:39,550 --> 00:04:46,060
So we only want to to see that when we execute this code directly, maybe we are testing these functions

57
00:04:46,060 --> 00:04:46,300
out.

58
00:04:46,300 --> 00:04:49,930
So we just want to, to work on these functions that p y.

59
00:04:50,290 --> 00:04:55,120
So the solution, as I told you, is to have a conditional block.

60
00:04:56,980 --> 00:05:03,660
And you say if underscore, underscore, name, underscore, underscore is equal.

61
00:05:03,670 --> 00:05:09,430
So double assignment operator is equal to quotes, open and close.

62
00:05:09,460 --> 00:05:12,940
Underscore, underscore, main, underscore, underscore.

63
00:05:12,940 --> 00:05:18,880
And then you put here a column and then these should be indented under that.

64
00:05:19,630 --> 00:05:21,010
Let me make a break line.

65
00:05:21,010 --> 00:05:23,040
So that's the if conditional.

66
00:05:23,050 --> 00:05:31,270
Now if I run the script functions that p y again I get that executed and that as well as you see the

67
00:05:31,270 --> 00:05:33,100
output in here and there.

68
00:05:33,400 --> 00:05:39,250
But if I execute main that P now you see that.

69
00:05:41,550 --> 00:05:43,990
These two lines were not executed.

70
00:05:44,010 --> 00:05:46,050
We don't have that code in here.

71
00:05:47,130 --> 00:05:49,190
These are executed.

72
00:05:49,200 --> 00:05:53,490
So the functions are defined when we import functions.

73
00:05:53,490 --> 00:05:56,640
And these functions definitions are available here.

74
00:05:58,750 --> 00:06:03,460
So everything that is outside that if conditional.

75
00:06:07,150 --> 00:06:10,720
Such as this print function is executed.

76
00:06:10,750 --> 00:06:19,960
If I execute main that we will see that we get that output because it's outside of that if conditional

77
00:06:19,960 --> 00:06:20,560
block.

78
00:06:21,130 --> 00:06:22,780
Now what is this?

79
00:06:22,780 --> 00:06:25,870
If conditional block, what does it do actually?

80
00:06:27,790 --> 00:06:29,460
Well, we have a variable here.

81
00:06:29,470 --> 00:06:30,970
This is a variable.

82
00:06:32,010 --> 00:06:40,740
But it is a variable which is defined hideously by Python and its value.

83
00:06:42,450 --> 00:06:48,450
And I'll show you what value this name variable has.

84
00:06:48,630 --> 00:06:52,470
So let me print that here.

85
00:06:53,760 --> 00:07:00,090
So print name and I'll execute this script functions.

86
00:07:00,090 --> 00:07:05,580
That's p y and that is the value of name.

87
00:07:05,580 --> 00:07:06,840
This is a string.

88
00:07:07,800 --> 00:07:09,210
So if you check the type.

89
00:07:11,140 --> 00:07:18,250
Of the value of the variable name, you'll see that it's a string and its value is.

90
00:07:20,240 --> 00:07:20,810
Maine.

91
00:07:21,860 --> 00:07:23,210
So that is a string.

92
00:07:25,910 --> 00:07:33,470
But its value is main only when we execute functions that p directly run functions.

93
00:07:34,520 --> 00:07:44,930
If you execute execute main that p y you'll see that the value of name is functions.

94
00:07:45,920 --> 00:07:51,440
So we get that printed out from that line.

95
00:07:52,440 --> 00:08:02,370
So to sum it up, when you run Maine, that's p Y and Maine does P runs functions that p y through that

96
00:08:02,370 --> 00:08:03,510
import statement.

97
00:08:03,630 --> 00:08:10,800
Then the value of name is the name of that file functions in this case.

98
00:08:11,430 --> 00:08:16,500
But when you run functions that p directly.

99
00:08:18,670 --> 00:08:25,090
The value of that variable is underscore, underscore, main, underscore, underscore.

100
00:08:25,120 --> 00:08:29,320
And that allows us to have this condition here.

101
00:08:29,500 --> 00:08:32,950
So if name is equal to that string.

102
00:08:34,789 --> 00:08:43,159
I mean, that means somebody is running this script directly, and that's somebody which is us.

103
00:08:43,190 --> 00:08:46,430
That somebody wants these lines to be executed.

104
00:08:46,790 --> 00:08:56,060
Otherwise, if I'm being imported, the file says, then I will not execute this because my name is

105
00:08:56,060 --> 00:08:58,010
not Main is something else.

106
00:08:59,190 --> 00:09:07,050
So that is the distinction we can make to control what we execute when the file is run directly and

107
00:09:07,050 --> 00:09:14,010
what we execute when the file is run indirectly through this import statement.

108
00:09:15,470 --> 00:09:22,550
This can have practical uses in certain scenarios, especially when you build web applications.

109
00:09:22,550 --> 00:09:29,090
So in that case, you might want to use that to have different outputs when you test the code and so

110
00:09:29,090 --> 00:09:29,450
on.

111
00:09:29,870 --> 00:09:36,170
So that's something you should know and I hope it makes sense.

112
00:09:36,200 --> 00:09:44,450
You'll see this if name is equal to main in other people's code, and this is actually one of the most

113
00:09:44,450 --> 00:09:46,960
asked questions on the internet about Python.

114
00:09:46,970 --> 00:09:50,310
People ask what is if name is equal to main?

115
00:09:50,330 --> 00:09:55,670
I hope this makes sense to you and you don't have the same question later on.

116
00:09:56,920 --> 00:09:57,460
Thanks a lot.

117
00:09:57,490 --> 00:09:57,970
See you later.

