1 00:00:00,000 --> 00:00:03,200 Let's talk about '__name__', and '__main__'. 2 00:00:03,200 --> 00:00:06,000 You're going to see this a lot in Python files. 3 00:00:06,000 --> 00:00:09,800 So often in a file, you're going to see something like, 'if 4 00:00:09,800 --> 00:00:18,200 __name__ == "__main__":', 5 00:00:18,200 --> 00:00:20,600 [no audio] 6 00:00:20,600 --> 00:00:25,700 and then 'print("Do a thing")', or maybe it runs some sort of 7 00:00:25,760 --> 00:00:28,860 function. Now, this is actually pretty important when it 8 00:00:28,860 --> 00:00:31,500 comes to running a Python script. 9 00:00:31,500 --> 00:00:34,200 And this is again, fairly common. 10 00:00:34,200 --> 00:00:36,500 You're going to see this quite a bit out there when you start 11 00:00:36,500 --> 00:00:38,100 looking at other people's Python code. 12 00:00:38,160 --> 00:00:42,360 Now, the idea behind this is to determine if the file is 13 00:00:42,370 --> 00:00:45,690 being used as, like an import for a module or a package, 14 00:00:45,900 --> 00:00:50,130 or if it's being used by a regular Python script, 15 00:00:50,130 --> 00:00:53,100 so if it's basically just supposed to run as is. 16 00:00:53,200 --> 00:00:55,800 So is that an import, or is it running like a program? 17 00:00:56,800 --> 00:00:59,700 So I'm going to create a new file, new folder in here, and, 18 00:00:59,700 --> 00:01:01,000 nope, not in there. 19 00:01:01,800 --> 00:01:06,200 And this is going to be called 'Name and Main'. Clear that. 'Name and 20 00:01:06,200 --> 00:01:07,180 Main', create a new file. 21 00:01:07,270 --> 00:01:09,310 And this one's just going to be called 'first.py'. 22 00:01:09,310 --> 00:01:11,600 And I just put it in a new folder just to sort of separate it 23 00:01:11,600 --> 00:01:12,600 from the other files 24 00:01:12,600 --> 00:01:15,400 so we don't get a little confused about which files do what. 25 00:01:16,300 --> 00:01:20,900 Now, how Python works, is you can sort of see this invisible 26 00:01:20,970 --> 00:01:23,330 line that comes up and down in my editor here. 27 00:01:23,980 --> 00:01:28,580 Any code that is the farthest left, Python is going to, 28 00:01:28,590 --> 00:01:29,510 it's going to try to run that, 29 00:01:29,510 --> 00:01:31,700 it's going to at least try to analyze it. 30 00:01:31,760 --> 00:01:37,700 So if we said variable 'name = "Kalob", and the 'course 31 00:01:37,710 --> 00:01:42,310 = "Python for Everybody", then these are going to 32 00:01:42,310 --> 00:01:43,400 be evaluated right away. 33 00:01:43,400 --> 00:01:48,310 But if we said 'def', some function name, 'print("a thing here")', 34 00:01:48,320 --> 00:01:51,280 the reason this automatically doesn't get run is because 35 00:01:51,280 --> 00:01:53,900 this code here is not touching this side. 36 00:01:53,900 --> 00:01:55,940 So Python is going to go through here, and say, "On line 2 37 00:01:55,950 --> 00:01:57,980 there's a variable, on line 3 there's a variable, and on 38 00:01:57,990 --> 00:01:59,480 line 5 there's a function." 39 00:01:59,620 --> 00:02:01,900 But this code here is not touching the wall. 40 00:02:01,900 --> 00:02:04,400 It's actually indented inwards. 41 00:02:04,450 --> 00:02:07,420 And because of that, Python says, "Okay, I understand that 42 00:02:07,430 --> 00:02:09,729 there is a function here, but I'm not actually going to run 43 00:02:09,729 --> 00:02:14,000 this". And in every Python script, there's always this variable 44 00:02:14,000 --> 00:02:16,070 called '__name__'. 45 00:02:16,900 --> 00:02:19,200 So let's go ahead and get rid of some of this stuff. 46 00:02:19,240 --> 00:02:24,040 And let's just print name, 'print(__name__)', and let's run 47 00:02:24,050 --> 00:02:25,150 this in our terminal. 48 00:02:25,160 --> 00:02:30,620 So 'cd' into the folder called 'Name\ and\ Main/', 'ls -la' or 'dir' 49 00:02:30,620 --> 00:02:31,600 if you're on Windows. 50 00:02:31,600 --> 00:02:37,100 And let's do 'python first.py', and it says, this '__name__' is '__main__'. 51 00:02:37,160 --> 00:02:46,110 So now we know, 'if __name__ == "__main__"', that this is a program 52 00:02:46,800 --> 00:02:52,410 calling the file, and we can say 'print()' in here, 53 00:02:53,900 --> 00:02:57,900 "This is a program running the file". 54 00:02:59,700 --> 00:03:03,400 And let's go ahead and run that code in our terminal just once more. 55 00:03:03,400 --> 00:03:06,300 Sure enough, "This is a program running the file". 56 00:03:06,300 --> 00:03:08,300 [no audio] 57 00:03:08,300 --> 00:03:10,700 But just for funsies, let's also add an 'else' statement in here. 58 00:03:10,700 --> 00:03:14,300 'else print("This is an import")'. 59 00:03:14,300 --> 00:03:18,000 And when we run this, we're going to see that nothing actually changes here. 60 00:03:18,000 --> 00:03:22,900 But if we go ahead and create another file, let's call it 61 00:03:22,970 --> 00:03:27,740 'second.py', and it's right beside my 'first.py', I can say 62 00:03:28,000 --> 00:03:34,000 'from first import', that's this file here, I can import something in here. 63 00:03:34,060 --> 00:03:40,020 So let's create a function called 'greeting()', and it's just 64 00:03:40,020 --> 00:03:44,100 going to say, 'print("Hello from first.py")'. 65 00:03:44,120 --> 00:03:46,670 Let's get rid of this 'print' statement, that's not useful anymore. 66 00:03:46,740 --> 00:03:52,500 And so in 'second.py', we are going to import that 'greeting()' 67 00:03:52,510 --> 00:03:53,970 from the first file here, 68 00:03:53,980 --> 00:03:57,900 'first.py', and because '__name__' is not going to be "__main__", because 69 00:03:57,910 --> 00:03:59,250 it's actually just an import, 70 00:03:59,260 --> 00:04:01,200 this should run for us, 71 00:04:01,200 --> 00:04:02,900 and it should say, "This is an import". 72 00:04:02,940 --> 00:04:06,780 So let's go ahead and do this, 'from first import greeting', 73 00:04:06,790 --> 00:04:08,310 and then execute the function, 74 00:04:08,310 --> 00:04:13,800 and let's run the script with, 'python second.py'. 75 00:04:13,880 --> 00:04:17,779 It says, "This is an import", because, hey, look at that, 76 00:04:17,790 --> 00:04:21,649 we imported the file before we did anything, and then we 77 00:04:21,649 --> 00:04:22,600 ran the script. 78 00:04:22,600 --> 00:04:25,200 And because of that, it says "This is an import", and then 79 00:04:25,399 --> 00:04:27,300 ran the function that we're looking for. 80 00:04:27,300 --> 00:04:29,300 [no audio] 81 00:04:29,300 --> 00:04:32,300 And again, if we run 'python first.py', 82 00:04:32,320 --> 00:04:36,590 says, "This is a program running the file", and if we run 'python 83 00:04:36,600 --> 00:04:39,600 second.py', it says, "This is an import". 84 00:04:39,650 --> 00:04:42,260 Now, you actually aren't going to see this 'else' statement too 85 00:04:42,260 --> 00:04:45,500 often. It's pretty rare that you see this kind of 'else' statement. 86 00:04:45,540 --> 00:04:47,520 Usually you just see something like this. 87 00:04:47,530 --> 00:04:50,340 And in fact, you won't even usually see a 'print' statement. 88 00:04:50,350 --> 00:04:55,450 But what you will see is if Python is calling this file as 89 00:04:55,460 --> 00:05:00,430 a program, you will often see a function being called in 90 00:05:00,440 --> 00:05:03,480 here. So this function is already defined, and what this 91 00:05:03,490 --> 00:05:05,760 is saying is just automatically run that function. 92 00:05:05,900 --> 00:05:11,600 So let's go ahead and run 'first.py'. 'python first.py'. 93 00:05:13,100 --> 00:05:15,100 And it says, "Hello from first.py". 94 00:05:16,300 --> 00:05:19,200 And that's because it is now executing this automatically. 95 00:05:20,200 --> 00:05:22,300 And in 'second', we don't actually have anything in here. 96 00:05:22,400 --> 00:05:26,500 So even if you run 'second.py' as a program, it doesn't really care. 97 00:05:26,520 --> 00:05:28,220 We're not telling you to do anything extra. 98 00:05:28,340 --> 00:05:31,400 And if you run it as an import, it's not going to do anything 99 00:05:31,400 --> 00:05:34,000 anyways, because it's just an import. 100 00:05:34,020 --> 00:05:37,230 So if you ever need to determine the difference between when 101 00:05:37,240 --> 00:05:41,510 a file is being run as a program versus a file being imported 102 00:05:41,510 --> 00:05:47,000 as a module or a package, this is the way you would want to do that. 103 00:05:47,050 --> 00:05:50,340 Now you might be thinking, "Why on Earth would I ever need 104 00:05:50,350 --> 00:05:55,160 that?" Well, let's say you have a little mini search engine 105 00:05:55,170 --> 00:05:58,420 or a little web scraper that you've built, and you have a 106 00:05:58,430 --> 00:06:02,620 function in here that scrapes a page, 'scrape_a_page', and it 107 00:06:02,630 --> 00:06:08,010 just goes ahead and gets all the images from a particular 108 00:06:08,010 --> 00:06:10,700 website. So it's going to work some magic in here, 109 00:06:10,700 --> 00:06:13,300 [no audio] 110 00:06:13,300 --> 00:06:18,100 and it's going to 'return "something", '#magic logic in here'. 111 00:06:18,600 --> 00:06:19,600 Now, that's fine. 112 00:06:19,600 --> 00:06:22,400 'scrape_a_page' might automatically want to be run as soon 113 00:06:22,400 --> 00:06:24,600 as you call 'python first.py'. 114 00:06:24,600 --> 00:06:27,900 Now, if you wanted to reuse this, you could copy this whole 115 00:06:27,910 --> 00:06:30,480 file into another project that you're working on, 116 00:06:30,490 --> 00:06:31,620 and you could simply import. 117 00:06:31,620 --> 00:06:35,600 You could say 'from first import scrape_a_page'. 118 00:06:35,640 --> 00:06:39,180 And because this isn't in here, it's not automatically going 119 00:06:39,190 --> 00:06:39,930 to scrape the page. So 120 00:06:39,930 --> 00:06:42,700 now you can actually use it as a regular import, but you can 121 00:06:42,700 --> 00:06:45,300 also use it as a regular program file. 122 00:06:46,300 --> 00:06:49,400 Now, what I would like you to do is test this out, create 123 00:06:49,430 --> 00:06:53,560 'first.py', 'second.py', and in your 'second.py' import from your 124 00:06:53,560 --> 00:06:57,600 'first.py', and have this '__name__ == "__main__"', and then 125 00:06:57,600 --> 00:06:58,600 run a function in here. 126 00:06:58,600 --> 00:07:02,400 Now I'm going to undo a bunch of this because that's not actually 127 00:07:02,420 --> 00:07:03,520 going to do anything for us. 128 00:07:03,760 --> 00:07:06,790 Go ahead, give this a shot just like I did in this video. 129 00:07:07,200 --> 00:07:11,080 You may or may not use this all the time, depending on sort 130 00:07:11,090 --> 00:07:13,510 of which industry you're going to go into with Python. 131 00:07:13,600 --> 00:07:16,900 But it is still good to know because you are going to see this 132 00:07:16,900 --> 00:07:18,900 regardless in Python code. 133 00:07:18,900 --> 00:07:19,933 [no audio]