Skip to content
Related Articles

Related Articles

Improve Article

Python nonlocal Keyword

  • Last Updated : 14 Sep, 2021
Geek Week

Python nonlocal keyword is used to make the variable which refers to the variable bounded in the nearest scope. Scope to which variable it bound should not be global or local scope. In Python nonlocal variable follows different behavior from identical binding, It searches for variables in another scope. The nonlocal variable is used in the nested function. nonlocal variable is used in declaring the variable.  

Advantages of nonlocal:

  • It helps in accessing the variable which is not in the same scope.
  • Since it makes a variable refer to another variable, it makes the variable more reusable and saves the memory for the new variable.

Disadvantages of nonlocal:

  • nonlocal variable is not used with the global and local variable.
  • nonlocal variable is not used for a variable which is not in nesting scope.

Demonstrating nonlocal variable:

Example 1: In this example, we demonstrate the working of nonlocal variables. We create the nonlcoal variable and change the content of it in nested scope, and affect the content of the variable in the upper scope. 

Python3




# Python code to demonstrait
# nonlocal keyword
  
# Nested function to demonstrait
# nonlocal keyword
  
  
def geek_func():
      
    # local variable to geek_func
    geek_name = "geek"
    # Inner function
  
    def geek_func2():
        
        # Declairing nonlocal variable
        nonlocal geek_name
        geek_name = 'GeekForGeeks'
          
        # Printing our variable
        print(geek_name)
          
    # Calling inner function
    geek_func2()
      
    # Printing local variable
    print(geek_name)
  
geek_func()

Output: 

GeekForGeeks
GeekForGeeks   

Example 2: In this example, we see what happens when we make a nonlocal variable to refer to the global variable.

Python3




# Python code to demonstrait
# nonlocal keyword
  
# Nested function to demonstrait
# nonlocal keyword
  
# Declairing variable in global scope
geek_name = 'geekforgeeks'
  
  
def geek_func():
    
    # Defining inner function
    def geek_func2():
        
        # Declairing nonlocal variable
        nonlocal geek_name
        geek_name = 'GeekForGeeks'
          
        # Printing our variable
        print(geek_name)
          
    # Calling inner function
    geek_func2()
      
geek_func()

Output:



SyntaxError: no binding for nonlocal 'geek_name' found

The possible application for the nonlocal variable is when accessing the variable of another scope without making a new variable it refers to a variable which is recently bounded to it. There is much possible application of nonlocal keywords. 

Example 3: In this example, we will see which variable nonlocal refer when we have multiple nested functions and they all have a variable with the same name.

Python3




# Python code to demonstrait
# nonlocal keyword
  
# Nested function to demonstrait
# nonlocal keyword
def geek_func():
  
    # local variable to geek_func
    geek_name = "geekforgeeks"
  
    # First Inner function
    def geek_func1():
        geek_name = "GeekforGeeks"
  
        # Second Inner function
        def geek_func2():
  
             # Declairing nonlocal variable
            nonlocal geek_name
            geek_name = 'GEEKSFORGEEKS'
  
            # Printing our nonlocal variable
            print(geek_name)
  
       # Calling Second inner function
        geek_func2()
  
        # Calling First inner function
    geek_func1()
  
    # Printing local variable to geek_func
    print(geek_name)
  
geek_func()

Output: 

GEEKSFORGEEKS
geekforgeeks

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :