1 00:00:00,380 --> 00:00:04,445 Hello again, in this video we're going to dig into Dunder main some more and 2 00:00:04,445 --> 00:00:06,120 why it's useful. 3 00:00:06,120 --> 00:00:09,848 Let's pop back into dm.py If you're not there already and 4 00:00:09,848 --> 00:00:11,610 look at our new Dunder main. 5 00:00:12,640 --> 00:00:18,110 Now the if here at the start lets us know that this is a conditional statement. 6 00:00:18,110 --> 00:00:23,438 It's checking this Dunder name or double underscore name value and 7 00:00:23,438 --> 00:00:27,280 seeing if it's equal to the string Dunder main. 8 00:00:28,530 --> 00:00:32,640 If it is, then what is inside of it will run. 9 00:00:32,640 --> 00:00:34,670 If it's not, then it will not. 10 00:00:35,740 --> 00:00:39,250 Now we can actually print out this Dunder name to see what it is. 11 00:00:39,250 --> 00:00:42,140 So I'm gonna put a couple of spaces here. 12 00:00:42,140 --> 00:00:43,870 And we're just gonna do a print statement. 13 00:00:45,140 --> 00:00:47,236 And let's do dunder name. 14 00:00:50,315 --> 00:00:53,830 dm.py's value. 15 00:00:53,830 --> 00:00:58,660 And then I'm just gonna copy this line and 16 00:00:58,660 --> 00:01:03,240 do the same thing over here in app.py. 17 00:01:03,240 --> 00:01:08,167 And then we gonna make sure we change this to app.py's value. 18 00:01:08,167 --> 00:01:09,750 Okay, perfect. 19 00:01:09,750 --> 00:01:14,693 So now we're gonna run both files and see what that Dunder name value comes out to. 20 00:01:14,693 --> 00:01:22,180 So I'm gonna start with running dm.py on its own, python3 dm.py. 21 00:01:22,180 --> 00:01:26,548 And you can see I get Dunder main is dm.py's value, 22 00:01:26,548 --> 00:01:31,750 which means this will evaluate to true, this conditional. 23 00:01:31,750 --> 00:01:35,130 So then this print statement gets printed to the console. 24 00:01:36,160 --> 00:01:40,060 Now let's check it out in app.py. 25 00:01:40,060 --> 00:01:43,361 I'm gonna make the terminal a little bit bigger here, okay 26 00:01:47,715 --> 00:01:52,869 Okay, so now you can see it's running dm.py first, so we still get that 27 00:01:52,869 --> 00:01:57,958 initial print statement where we're checking the Dunder name value. 28 00:01:57,958 --> 00:02:04,007 And you can see dm.py's named value is now dm, which is the name of our file. 29 00:02:04,007 --> 00:02:07,026 Which makes our conditional no longer true, 30 00:02:07,026 --> 00:02:10,370 which means this print statement no longer runs. 31 00:02:11,750 --> 00:02:15,048 Then it comes over here to our app.py and 32 00:02:15,048 --> 00:02:20,990 it prints out this line saying that app.py's value is now Dunder main. 33 00:02:20,990 --> 00:02:25,720 And then we also get our print statement from inside of this file. 34 00:02:25,720 --> 00:02:29,778 So when you run a file In the console directly, 35 00:02:29,778 --> 00:02:34,620 its Dunder name value becomes Dunder main. 36 00:02:34,620 --> 00:02:37,411 So that is how we use Dunder main or 37 00:02:37,411 --> 00:02:43,097 this conditional statement to control what executes in your code so 38 00:02:43,097 --> 00:02:49,100 only the elements you want to run can run and those you don't, won't. 39 00:02:50,930 --> 00:02:54,100 Let's talk best practices with an example. 40 00:02:54,100 --> 00:02:58,007 In modules or files that are meant to be imported, 41 00:02:58,007 --> 00:03:03,370 you wanna hold most of your code inside of functions or classes. 42 00:03:03,370 --> 00:03:06,585 And I'm gonna model this with a simple recipe. 43 00:03:06,585 --> 00:03:09,731 We're just gonna make a pretend peanut butter and jelly sandwich. 44 00:03:09,731 --> 00:03:14,280 So I'm gonna call this one, 45 00:03:14,280 --> 00:03:18,267 gather_ingredients. 46 00:03:18,267 --> 00:03:22,251 And I'm just gonna do a print 47 00:03:22,251 --> 00:03:27,398 statement that says Bread, jelly, 48 00:03:27,398 --> 00:03:32,559 and peanut butter gathered, okay? 49 00:03:34,752 --> 00:03:39,570 And then we're gonna do a second one and I'm gonna call 50 00:03:39,570 --> 00:03:44,507 it combine_ingredients to another print statement. 51 00:03:44,507 --> 00:03:49,517 And inside of here we're gonna do, 52 00:03:49,517 --> 00:03:52,523 Add peanut butter and 53 00:03:52,523 --> 00:03:58,039 jelly to slices and put them together. 54 00:03:58,039 --> 00:04:03,687 Awesome, now inside of our Dunder main, if I wanted to I could 55 00:04:03,687 --> 00:04:09,014 test out my functions by putting gather_ingredients and 56 00:04:09,014 --> 00:04:13,703 combine_ingredients function calls down here. 57 00:04:13,703 --> 00:04:17,434 So that while I was playing around or testing or 58 00:04:17,434 --> 00:04:22,313 just working on my code in this file, when I run it directly, 59 00:04:22,313 --> 00:04:25,583 you can see I get the correct responses. 60 00:04:25,583 --> 00:04:27,780 But if I run app.py, 61 00:04:32,602 --> 00:04:36,837 I don't get anything printed to the console from dm.py, so 62 00:04:36,837 --> 00:04:39,290 it doesn't clutter up my console. 63 00:04:40,410 --> 00:04:46,011 Now, another best practice is to place all function 64 00:04:46,011 --> 00:04:51,113 calls inside of another function called main. 65 00:04:51,113 --> 00:04:53,451 So it would look something like this. 66 00:04:57,274 --> 00:05:05,404 And then we would switch these two out, For main. 67 00:05:05,404 --> 00:05:08,727 And now if I run it in the console, I'm just gonna do up arrow. 68 00:05:08,727 --> 00:05:12,518 I'll get the same response from dm.py and app.py. 69 00:05:12,518 --> 00:05:17,540 Exact same thing, nothing from dm.py runs because this is inside of our Dunder main. 70 00:05:19,370 --> 00:05:20,293 Now this file, 71 00:05:20,293 --> 00:05:25,490 our dm.py can be imported without the worry of something running on its own. 72 00:05:25,490 --> 00:05:29,352 But like you saw, we can also run this file by itself to make sure things 73 00:05:29,352 --> 00:05:32,580 are working correctly inside of dm.py. 74 00:05:32,580 --> 00:05:37,676 The goal of Dunder main is to keep your console and program under control. 75 00:05:37,676 --> 00:05:43,976 Inside of dm.py or sorry, inside of app.py, we could now, if I remove these, 76 00:05:43,976 --> 00:05:49,091 I can either do dm.gather_ingredients to call that function. 77 00:05:49,091 --> 00:05:54,073 Or to make things even simpler, I now only have to call this 78 00:05:54,073 --> 00:05:59,280 one function and it runs everything inside of dm.py for me. 79 00:05:59,280 --> 00:06:02,188 So now if I do, let me clear the console, just make this. 80 00:06:02,188 --> 00:06:04,202 Oops, forgot to write the whole word. 81 00:06:04,202 --> 00:06:05,051 There we go. 82 00:06:07,393 --> 00:06:12,060 Now, if I run app.py you'll see, I get those two statements printed out. 83 00:06:13,570 --> 00:06:19,117 But you can see we call our function main here inside of our Dunder main, 84 00:06:19,117 --> 00:06:24,293 which also calls those two functions that print those things out, 85 00:06:24,293 --> 00:06:29,377 those statements, but they don't get printed out to the console 86 00:06:29,377 --> 00:06:35,860 because Dunder main is saying, hey, this is not true, so don't do this. 87 00:06:35,860 --> 00:06:40,697 So now we only have two statements printed to the console instead of getting 88 00:06:40,697 --> 00:06:42,170 cluttered with four. 89 00:06:42,170 --> 00:06:46,399 So what I'm saying is if we didn't have this by comment this out, 90 00:06:48,648 --> 00:06:52,096 And then run app.py, you can see now we get four printed out. 91 00:06:52,096 --> 00:06:53,789 And you can imagine over time and 92 00:06:53,789 --> 00:06:57,863 the larger your program is that would become really annoying to have all these 93 00:06:57,863 --> 00:07:00,634 statements coming from all these different files. 94 00:07:01,943 --> 00:07:04,980 So instead, we use Dunder main to keep things under control. 95 00:07:06,670 --> 00:07:10,291 So in the next video, I'm going to give you a challenge to try and 96 00:07:10,291 --> 00:07:13,220 incorporate Dunder main on your own. 97 00:07:13,220 --> 00:07:13,720 See you there.