Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 4: Journal app and file I/O
Lecture: Using __name__ to selectively execute code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Finally let's briefly turn our attention to this line 51 here, main(). So I told you calling main like this is not ideal,
0:09 but I've been pushing off talking about why that is and it really has to do with when your code is used as an external module to other code, right?
0:20 Either code you write or if you are writing a package, people who consume your package and let's see what the problem is.
0:26 Let me just create a program 2. Now over here in this program 2 suppose we would like to do this print header thing up here at the top.
0:40 This print header method, it's unlikely you would import that whole thing just to get this but go with the idea.
0:47 Imagine I want to use this print header method up here so I would like to do something like let's just say import program great,
0:55 and I could say program.print_header(). Now let's make this super obvious what is happening in terms of the program flow, so I'll do a print,
1:03 and say about to import program, let's call it step 1 down here we'll do a step 2 program imported, and then maybe a step 3 printing header
1:24 and we'll add a step 4 down at the very end here, done... done with program. Now, if you think how we want this to work
1:33 it should print about to import program, it should perform that silently, then it should print program imported,
1:42 then it should print printing the header and we have the little header part, remember it's going to say --- journal app--- and then it should say done.
1:51 And it should just run without the user input, conceptually the way we are trying to run it. However, that is not what is going to happen,
1:58 so let me run this really quick, now it says about to import program and we see the header, ok, that is a little early,
2:03 but let's see what else is going on, now it says what do you want to do with your journal, and I can apparently interact with it- weird,
2:14 ok, that was pretty unexpected, if I hit exit it exits and then it says ok now the program is imported
2:21 so all of this from here to there, that is literally line 2 of our program, what the heck is going on?
2:30 Well, remember the way Python defines classes, functions, variables and so on is it literally executes this file, top to bottom.
2:41 It says ok we are going to execute importing the journal, all the stuff that that means we are going to execute defining main,
2:46 we are going to execute defining print header and so on, so it's all looking fine until we get to here, this actually has side effects,
2:54 this actually starts that event loop that we saw going around and around so in order to use this, I kind of need to not call it when we are importing
3:03 so here it worked fine, now that I commented that out, it worked the way we expected. So, in Python there is a convention that allows us to say
3:12 if you are trying to run this program do some extra code but if you are trying to be a library, just define the features the variables functions,
3:21 classes and so on during import. It all has to do with this special implicit variable called _name_
3:30 there is several implicit values that we can get out of this module, one is the actual full path to the file that is executing,
3:37 that can be really handy when you are trying to find something relative to the current module, and this name,
3:43 this one actually is the convention we are going to use, so let me just run this, remember we are running program 2,
3:50 we are going to import this, you'll see that this should be program and this should actually be the full path to program.
3:59 Here you can see that's the __file__, and here you can see that is __name__, let's make this super explicit.
4:11 So I'll put __file__, __name__, then I will run it and you will see the files that program is that could use a space, but you get the idea.
4:19 Ok, now let's go over here, let me add those spaces, and let's go over here and change the app that we are running, we are going to run program itself,
4:28 so when I run program, the file did not change but check out what happened to the name, it became __main__
4:35 so the convention in Python is when you are running a script if it's the thing the target of execution, its __name__ is set to __main__.
4:46 If on the other hand it's being imported, it's just the name of the Python file typically without the extensions. Ok, so how does that help us?
4:56 Well, down here in main, we can do this, we can say if, __name__ is this main then in that case, that means we are trying to run this program,
5:07 right now so let's kick off the main(). On the other hand, if it's being imported, if this is program rather than main(),
5:15 just don't execute our code, just let it define the functions and variables. So if I run program, that's this file right now,
5:21 it should be main, so it should act like our original app. Right, it lets us list entries and exits. On the other hand, if I am trying to import it,
5:30 whereas I am running this program, it's not going to be called __main__, it's going to be called program,
5:37 so let's run that, now notice file is of course what it is, name is program we did not call main() so it worked exactly like we wanted,
5:46 1 about to import program, boom- done importing, printing the header, so if we did not have this little print bit here,
5:55 it would act exactly like you would have initially expected program 2 to run.

Talk Python's Mastodon Michael Kennedy's Mastodon