Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Main method
0:00 Before we get into writing the code that goes behind each one of these steps, let's do just a little bit of organization.
0:06 Remember, I always like to structure my program with a main method that has the high level steps of what it's going to do right the top.
0:13 So let's write that, and we'll put these steps here. Now, notice there's a little error right there, if I make it go up
0:19 it'll probably move to the bottom. No, it still stays there. The error is that the main method doesn't actually do anything yet,
0:25 so let's just make it do something. "Hello from weather main". Okay,
0:32 so now there's a meaningful main implementation here so that the structure of the Python code actually works. However, remember,
0:39 if we run it, there's no output. What happened to our "hello weather main"? So that's because we're not actually calling it, right.
0:46 That's just defining a function that if it were to be called, it would say "hello from weather main". So if we call it like this,
0:51 that's great. However, this will not allow us to reuse the functionality of this script or this application. So, like get a report from the API?
1:01 Nope. We will not be able to use that function because if we import this, it's going to actually, as part of the import statement,
1:07 processing, is going to run this. So, recall there's this convention, "if __name__ == '__main__'" that means it's being run explicitly,
1:17 not imported, so we should do the same thing. But if somewhere else we import it, it'll just define the functions and not try to run the program.
1:24 This actually is so common, this convention right there, in most real Python programs, that PyCharm actually has what's called a "live template"
1:34 for it. So if I type the word "main" you can see there's this thing that's a function, that's just what I wrote above,
1:40 but there's another one down there with a little hint that's like "if name equals main"
1:44 hmm, maybe that's interesting. So if we hit Tab here it's going to expand that,
1:49 that live template will expand that, and then we can call the actual function we're
1:54 looking for, Okay? And it doesn't matter what this is called. This could be called something like "entry point" or whatever,
2:02 right? It just means if we're being run as a program, run the thing that we're thinking of as the top level. Here we go. Okay, So let's look over here.
2:14 And if we look actually in the editor, you can see there's a whole section of these live templates for all the different languages.
2:26 is Python. And down here you can actually see "main". So this is the template that we were expanding, and I actually went
2:34 and created my own that will create the main method and write that. So I can just start from an empty file and type "F-main" so you can create new
2:41 ones as you see fit. So, for example, the one I created would do this part right here.
2:47 Saves me a little bit less thinking about it, just get started and go. So these live
2:51 templates are super helpful, and now our program is ready to be run as an application.