Ask for the Addresses (Solution)
00:00 Here I am in IDLE. On the right side, you see the IDLE editor, and on the left side you see the IDLE shell where I’ll run the program in a moment. But before running it, let’s go through the code.
00:13
On lines one to nine, you see the function definition of explore_basement()
. There are two print()
function calls inside of it, but when you define a function in Python, it’s not executed yet.
00:26
So let’s move on to line 12 where you define the variable address
, which says “Python Palace”, and then only on line 13, you actually call the explore_basement()
function.
00:40
Now you can jump back into line one and explore the explore_basement()
function, what’s happening there. In line two, inside the explore_basement()
function, there is an explore_cabinet()
function until line five.
00:56
So again, anything inside of it is not executed yet. Python only registers, okay, there is an explore_cabinet()
function I should know about, but unless it’s executed, I basically don’t care that much.
01:08
So again, let’s jump ahead now to line seven where you define another address
variable, which is called “Mouse House”. And then you call the explore_cabinet()
function in line eight.
01:22
So now you can look what’s inside the explore_cabinet()
function, which is in line three, the mouse docstring, “Hello Mouse”. And in line four, another address
variable with the string “Cookie Cabinet”.
01:35
And then in line five, the print()
function call of address
. So that’s actually the moment where the program for the first time calls the print()
function and you call the print()
function with the address
variable.
01:50
Now there are multiple address
variables in this code. Think a moment which address
variable Python would take at this place.
01:59
When Python looks for names, it starts with the local scope. And the local scope right now is the explore_cabinet()
function. So the address
variable in line four is taken first, which is “Cookie Cabinet”.
02:13
So this will be the first output. Then let’s jump ahead to line nine. It’s the line after the explore_cabinet()
function call. And here again you have a print()
function call.
02:24
And again, you pass in address
. At this point, the local scope is the explore_basement()
function. So the address
variable now is not the one that was in explore_cabinet()
, which was “Cookie Cabinet”, but it’s the one in the explore_basement()
function, which is “Mouse House”.
02:43 So the second output will be “Mouse House”.
02:47
And with this, you basically executed the explore_basement()
function and you return to line 13 and can move to the next line, which is the last print()
function call.
02:59
And again, it’s calling address
. And now you look for the address
in this scope, which is basically now the local scope, but also the global scope.
03:09
So there might be a bit of a mind twist there. And you pass an address
into the print()
function call. And in the same scope of this print()
function call is the address
variable in line 12, which is “Python Palace”.
03:23 So the output should be “Cookie Cabinet”, “Mouse House”, and “Python Palace”. Let’s try it out. You can run Python files in IDLE by clicking the run menu item and clicking run module or using the F5 shortcut.
03:40
And as you can see, the output is “Cookie Cabinet”, “Mouse House”, and “Python Palace”. Coincidentally, the output is exactly the order, which the variables are defined in the Python code, line four, line seven, and line 12, or the print()
function calls after that.
03:59 But that’s not always the case. So don’t get confused by this one but follow the Python program through the scopes to know when Python outputs what. And in this case, it’s “Cookie Cabinet”, “Mouse House”, and “Python Palace”.
04:13 These are fun names and great places to have a party, in my opinion.
Become a Member to join the conversation.