Transcript from the "Function Scope" Lesson
>> Nina Zakharenko: I've used the word scope a little bit. Functions have scope, and when we`re inside of a Python function, that scope changes. You can kind of think about it this way, that Python scoping happens with white space. When we delineate the code block for a function, we indent it under the function definition.
[00:00:20] And within that block of indented code, its scope changes to a new internal scope that just exists for the function. In that internal scope, the function has access to variables that are defined outside of it. But it can't actually change them. So just something to keep an eye out for.
[00:00:47] Let's say I had a function here called twitter_info. And I'm gonna say that my account is nnja. And then I'm gonna print out an f string and say, Account inside the function is,
>> Nina Zakharenko: If I call my function, we'll see that the Account inside of the function is nnja.
>> Nina Zakharenko: What if I try to access the variable account outside of the function? What's gonna happen?
>> Student: It doesn't exist.
>> Nina Zakharenko: Come on, you guys know the answer.
>> Nina Zakharenko: Right, exactly right, the account is not defined. Why is account not defined? Because we declared it in the scope of this function, right?
[00:01:56] So when we run this function, we have access to this account variable. Outside of this function, we're in a different scope, and there is no account variable. Let's say I declared a variable outside of the function.
>> Nina Zakharenko: And I'm gonna make a different function. I'm gonna say try to change the name.
[00:02:27] And within this function, the name is Max, and I'm gonna print out an f string that says Name inside function.
>> Nina Zakharenko: Okay, if I run this function, try_change_name, what is gonna print out?
>> Nina Zakharenko: What name?
>> Nina Zakharenko: That's right. Max, right. Even though I declared my name here inside of this function, the name is Max.
[00:03:07] I can also print out what's the name outside of the function.
>> Nina Zakharenko: What will this print out, which name?
>> Student: Nina.
>> Nina Zakharenko: Nina, that's right.
>> Nina Zakharenko: Okay, makes sense, right?
>> Nina Zakharenko: We don't necessarily really want to follow this pattern. We don't want to change defined variables outside of their defined scope.
[00:03:43] So in general for you Python programs, you don't necessarily want to, or like I'm talking about production Python programs, right? Not when you're first starting out, generally don't wanna have too many variables floating around outside of a defined scope. Maybe something like a constant, like a variable that doesn't change over the execution of the program, right?
[00:04:06] It's okay to define it out of the scope, but just kind of be mindful of this. It seems like there is a little bit of confusion about having an empty list as default argument. So just to be very clear on that, I am saying don't do this, don't do it.
[00:04:28] It's the source of a lot of bugs. I'm giving you an example of what not to do, just so that you know what the behavior is.
>> Nina Zakharenko: So I defined this function foo, that takes a and provides an empty list as a default argument. It doesn't really have anything to do with scope, right?
[00:04:59] The reason that b is the same list every time I call this function, is because these default arguments, they get instantiated once, when the function is defined. So when you're using kind of these basic types integer string, the safe types to be considered default arguments. It doesn't really matter right?
[00:05:24] If it's the same instance, but with an empty list, there's just kind of one handle on this empty list. So if you think that this is going to make a new empty list every time you call this function, you're gonna be wrong, right? And it's the kind of thing where you're not gonna get a syntax error.
[00:05:45] You're not gonna get any sort of hint. You're just gonna end up with some really weird, hard to diagnose bugs, right? So just to be very clear, don't do this. Keep an eye out for this, right? So I have my list b, I'm appending a, and then I'm printing out the value of b, right?
[00:06:09] As I called foo, when I call it once, okay, cool, right? I have my empty list b, I appended value a too and I'm printing it out. That does what we expect, right? But when I call it again, b didn't, when I call this function, this b variable was not cleared, right?
[00:06:30] Instead it's just getting upended to over and over, so just a subtle cause of bugs. Instead, I can show you what you would want to do right? So
>> Nina Zakharenko: Do this instead. You can say def foo a and b. You'd wanna say b to the none type, right.
[00:06:54] That's kind of like a flag or a signal for us. And then sorry, I'm gonna use some control statements and stuff, we'll talk about these soon, but then I can just say If b is none, then make an empty list, right? And then b.append(a) and printing b. Don't worry, I'll cover all of this in more detail soon, right.
[00:07:23] But now, if I call foo with 1, get 1. If I call foo with 5, I get an empty list with 5 in it, right? This does the correct behavior, okay? So I hope that clarifies things for everyone. Just don't do this.