Add Local Variables
00:00
Let’s explore the value of the locals()
function inside the visit_woods()
function. Right after the bear
docstring in the line just before the if
statement, add in a print()
function call and pass in a locals()
function call, and then save and run the file.
00:23
Now you have two outputs there. There is still the big one from the locals()
function call in line nine that you’re printing, but there is also an empty dictionary just before that.
00:35
So that means that locals()
gives back an empty dictionary in line three because there is no local variable defined at this point. Knowing that, let’s be a bit cheeky and define a my_invitation
variable with the string, my_invitation
as a value inside the visit_woods()
function. Knowingly, this is not a solution for the exercise yet, but let’s see what happens if we define this variable just before the print()
function call with locals()
passed in.
01:09
In line three at the new variable my_invitation
equals the string, “my_invitation”, and then save and run the file.
01:21
And maybe for better visibility, let’s remove the print()
function call in line 10, so we only get the output of the print()
function call inside the visit_woods()
function.
01:33 Let’s save and run it again.
01:37
And now you can see that there are only two lines outputted. One is a dictionary with my_invitation
as a key and my_invitation
as a value.
01:47
So that’s the content of locals at this moment. And then you also get the my_invitation
variable outputted because that’s the value of my_invitation
.
01:59 And I thought I’d be a bit cheeky here by short-cutting the solution, but actually we want to print, “Let’s have a party!” so it’s not even the right value.
02:09 And also it’s very important for the bear, apparently, to get the invitation handed over personally. So let’s do that next.
02:18
But before doing so, let’s remove the my_invitation
, variable definition in line three to not confuse ourselves in the next lesson.
Become a Member to join the conversation.