Let's Talk About Scope3:26 with Ashley Boucher
Learn about the difference between global and local scope.
Welcome back. 0:00 Now that you now to define and call a function, 0:01 we have to expand our discussion a little bit to talk about something called scope. 0:04 Scope refers to the visibility of parts of our program. 0:08 Not all parts of a program can be seen by other parts of our program. 0:12 This is really important to remember when you're thinking about functions. 0:15 The code you write inside of a function is only accessible inside that function. 0:19 That means if you assign a variable inside a function, you can't use that variable 0:24 in another part of your program and expect it to reference the same value. 0:28 This can be a little hard to grasp without seeing it. 0:32 Let's jump into some code. 0:35 Okay, take a look at my workspace here. 0:37 You see we have a variable assignment num=10. 0:39 Then you see a function set_num. 0:43 Inside the set_num function there's another variable assignment. 0:47 Num=5 after the function definition the program calls set_num. 0:50 What do you think will be printed when this program is run? 0:57 Pause the video here. 1:00 Open up the attached workspace, and run the file in the workspace terminal, 1:01 see what's printed, and then come join me again. 1:06 All right, how did it go? 1:09 Were you a little surprised by the value that was printed? 1:10 Let's break this down. 1:13 The variable assignment num = 10 takes place inside 1:15 what is known as the global context, or the global scope. 1:19 It's global because it's not inside any other function, 1:23 it's at the highest level in the program. 1:26 Any other part of this program can see, access, and 1:28 use this variable including the code inside the set_num function. 1:32 So when the set_num function is called and 1:36 Python encounters the num = 5 variable assignment, it doesn't reassign num, it 1:38 creates a new num that only exist inside the local context of the set_num function. 1:44 That means that outside of set_num, even if we've already called it, num will 1:50 always reference 10 until this reference has changed in the global context. 1:54 Inside the set_num function however, num will have a value of five. 1:59 Additionally, if we create a variable inside set_num, 2:05 we can't access it outside of that function. 2:08 I'll create a new variable inside the function called letter, and 2:11 it'll reference the letter a. 2:14 Let's see what happens when I try to print the letter variable outside of set_num. 2:21 I'm gonna save, run this down in my terminal. 2:28 Okay, we're getting an error here. 2:37 A name error that says name 'letter' is not defined. 2:39 Well, that's because there is no letter variable inside the global scope. 2:44 Letter only exists inside the set_num function and 2:48 can only be accessed inside of it. 2:52 If we move our print statement inside the function and try again, 2:54 we'll probably get a different result. 2:57 Now I'm gonna save and try it again. 3:03 Cool, it printed the letter a. 3:08 So now that you know a little bit about global and local scope, 3:10 you might be wondering how you actually get values and variables out of functions. 3:13 A variable's created inside a function, in the local scope, 3:18 can't be seen in the global scope, then what's the point. 3:21 Stay tuned to find out. 3:24
You need to sign up for Treehouse in order to download course files.Sign up