Conditional Check4:31 with Ken Alger
We can utilize a conditional check to verify if our script is being run directly or being imported. Then we can handle those conditions differently based on our needs.
Let's start by creating a new file in our workspace called app.py. 0:00 For our example here, I'll define the symbol method that just prints a little 0:08 hello statement from our app. 0:12 And then call the method to print that statement. 0:14 We'll print 0:22 Hello from app. 0:27 Save our file. 0:36 Go to the Console. 0:38 And when we run our app, as expected, we get our output., great. 0:41 Let's create a second file, we'll call it second_app.py. 0:47 In here, we'll import app, And 0:57 then we'll add another short print statement, Well, from second app. 1:01 We can save that, And run it. 1:10 Let's see what happens now with that import. 1:16 Well, that's not entirely what we want. 1:20 We really don't want to be calling print hello just by importing app.py. 1:23 This is where we can utilize that conditional check with dunder name. 1:27 Let's head back into app.py and add that. 1:32 So right above our print_hello call, We'll add, 1:37 if __name__, == __main__. 1:43 Come over here, indent. 1:52 Now Python will check to see if app.py is the main module being run, or 1:55 if it is being imported. 2:00 Let's double check this from the command line to make sure 2:03 things are working as we have intended. 2:06 We run python app.py again. 2:10 Nice, that still works. 2:13 And if we run second_app.py, 2:16 That's better. 2:22 And since we've imported app.py into second_app.py, 2:26 we can call our print hello function when and if we need it. 2:29 App_print_hello, 2:34 Save it. 2:44 Let's clear our console here, And run second_app.py again. 2:46 Great, so we have access to print hello from our second app. 2:56 Let's take a quick peek at dunder name by adding another print statement to app.py. 3:00 Well print __name. 3:06 Now, when we run our scripts, we can see the value of dunder name, 3:08 based on if the module is run directly or imported. 3:16 We'll run app.py and see dunder_name is equal to dunder_main. 3:24 And if we run second_app.py, We see that dunder_name is equal to app. 3:33 Notice here as well that by having the print dunder_name statement in app.py 3:41 outside of a function, that command is run first inside second_app.py. 3:47 This is another important reason to have statements inside methods or 3:53 inside a conditional check for dunder name when using imports. 3:58 Along your journey as a Pythonista, you'll find many recommended best practices. 4:02 This conditional check is a common part of most Python programs and 4:08 is used in all sorts of Python applications 4:13 from trade Python scripts to Python frameworks like Flask and Django. 4:15 With this best practice in place, you can provide for 4:20 better management with what is happening in your scripts. 4:23 I look forward to seeing what you build. 4:27 Until next time, happy coding. 4:29
You need to sign up for Treehouse in order to download course files.Sign up