Python nonlocal keyword
If you are unable to think clearly about nonlocal keyword, you are at the right place. This article will provide you brief information about the use of nonlocal factors, and how they are unique in relation to neighbourhood and worldwide factors.
The nonlocal is a reserved keyword of Python, which is used in nested functions whose local scope is not defined. The necessity is that the variable shouldn't belong to the inner function's scope, therefore we define it in the outer function and use it inside the inner function as a nonlocal variable. This implies that the variable can be neither in the local nor the global scope. The nonlocal keyword is used to indicate that a variable is in the nonlocal scope. It binds one or more variables to the outer scope.
Syntax of nonlocal keyword
Example of nonlocal keyword
Here is the python program to demonstrate the example of nonlocal variable. In this, we have defined two variables x and y in both the outer function and the inner function. We are binding variable x as nonlocal in the inner_func() function, that's why x is not local to the inner function and update with newly assigned value, but y will be considered as a local variable for inner_func().
Output of the above program
# python code to show an example # of nonlocal keyword # nested functions def outer_func(): x = 20 y = 40 # nested inner function def inner_func(): # nonlocal variable binding nonlocal x x = 100 y = 200 print("Inner variable x : ", x) print("Inner variable y : ", y) # calling inner function inner_func() print("Outer variable x : ", x) # x updated as nonlocal print("Outer variable y : ", y) # y is considered as local variable # main code outer_func()
Here is another Python program that demonstrates the nonlocal keyword. It is the same as the above, but the variables x and y are of type strings.
Output of the above program
# python code to show an example # of nonlocal keyword # nested functions def outer_func(): x = "local variable x" y = "local variable y" # nested inner function def inner_func(): # nonlocal variable binding nonlocal x x = "Non local variable x" y = "Non local variable y" print("Inner variable x : ", x) print("Inner variable y : ", y) # calling inner function inner_func() print("Outer variable x : ", x) # x updated as nonlocal print("Outer variable y : ", y) # main code outer_func()
Count consonants in a string Python
Remove last element from list Python
Find the stop words in nltk Python
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text extraction from image
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML