Here is a discussion of two more keywords in Python. These are less well-known and widely-used than a lot of the other Python keywords but have their uses in specific circumstances:
To understand this term, you must realize that a variable that is changed inside a function will not be recognized outside the function. Let’s imagine a celebrity visits Vegas and goes into serious bout of debauchery, adding an extra 100 pounds to his usual trim frame of 150 pounds. Now, check out the following code:
def celeb_change(): celeb = 150 celeb = celeb + 100 return celeb
print (celeb_change()) #this will print 250
print (celeb) #but this will give an error message, saying, #“NameError: name 'celeb' is not defined”
When we print the function called celeb_change(), it’ll tell us celeb is equal to 250. But if we then ask Python what the value of celeb is outside the realm of that function, it won’t have a clue. Any value changes only happen locally. It’s sort of like, “What happens in Vegas, stays in Vegas.” Except, in this case, it’s what happens in a function stays in a function.
But we can change this. We can, in essence, broadcast the changes that were made in the function, saying, “Hey, we gave a value to celeb within a function but we want this to be remembered outside of that function.” I suppose it’s like some paparazzi taking a video of that celebrity after he gained the extra 100 pounds and and uploading it to YouTube. Suddenly, what happened in Vegas did not stay in Vegas. Here’s how you can do that:
def celeb_change(): global celeb #this line makes the existence and value #of celeb known outside this function celeb = 150 celeb = celeb + 100 return celeb
print (celeb_change()) #this will still print 250
print (celeb) #but this will also print 250 now because #the celeb is known to have a value of #250 even outside the function
So, then, what in the heck is nonlocal? Well, let’s say we have a local Vegas celeb who wanders out of his neighborhood but isn’t really going far: just over to another nearby neighborhood. The local paparazzi don’t worry if he gets out of line because they know he’s not-quite-local but not a complete stranger to Vegas either. Here’s some code to help us understand. Before you run it, though, “comment out” the prior code (to do this in IDLE, you can use Alt-3).
def big_celeb(): celeb = 250 print('heavy celeb weighs: ', celeb) def normal_celeb(): nonlocal celeb celeb = celeb - 75 print('normal celeb weighs: ', celeb) def skinny_celeb(): nonlocal celeb celeb = celeb - 25 print('thinner celeb weighs: ', celeb) skinny_celeb() normal_celeb() big_celeb() print ('celeb outside the function is: ', celeb) #the print statement above will throw an error code like this: #NameError: name 'celeb' is not defined #That's because celeb is nonlocal rather than global
This is what will print out:
heavy celeb weighs: 250 normal celeb weighs: 175 thinner celeb weighs: 150 Traceback (most recent call last): File "C:/Python34/global and nonlocal.py", line 34, in <module> print ('celeb outside the function is: ', celeb) NameError: name 'celeb' is not defined
What you see here are some nested functions. That just means, of course, that one function contains another function as part of it (and, in this case, there’s a third function is nested in there as well). Now, we want each of these three functions to “remember” that there’s a variable called “celeb” and its value the last time it was used. If we didn’t stick the words “nonlocal celeb” into the second and third functions, Python would throw an error. (Try it and see). We’re saying, “Hey, you nested functions, remember that this is the same celeb as the one mentioned in the previous part of the function.”
But, this does not mean that celeb will still be recognized outside the three nested functions. That is, the variable has not gone global. So, what happened in Vegas still stays in Vegas.
For more on global and nonlocal, see GeekforGeeks.