Tuesday, February 2, 2021

Nonlocal Keyword in Python With Examples

In this post we’ll see what is nonlocal keyword in Python and how to use it.

To understand nonlocal keyword better an understanding of local, global and nonlocal variables is required, as a requisite please go through this post- Local, Nonlocal And Global Variables in Python

Python nonlocal keyword

The nonlocal keyword causes the variable to refer to previously bound variables in the nearest enclosing scope excluding globals. Note that nonlocal keyword is added in Python 3.

Python nonlocal keyword is used with variables in nested functions to bind the variable defined in outer function, by making variable nonlocal it can be accessed in nested function.

Let’s try to understand it with some examples.

In the example there is a function myfunction() with a variable x defined in it and there is a nested function infunction() with a variable x defined with in its scope.

def myfunction():
  x = 7
  def infunction():
    x = 20
    print('x inside infunction', x)

  infunction()
  print('x inside myfunction', x)

myfunction()

Output

x inside infunction 20
x inside myfunction 7

As you can see value of x is printed as per the scope it is defined in.

Now, consider the scenario where you want to access the variable defined in outer function. You may think of using global keyword to access variable in global scope but the problem is even the outer function variable is not deemed global as it is with in a local scope of a function.

def myfunction():
  x = 7
  def infunction():
    global x
    x = 20
    print('x inside infunction', x)

  infunction()
  print('x inside myfunction', x)

myfunction() 

Output

x inside infunction 20
x inside myfunction 7

As you can see using global keyword doesn’t help in this scenario.

Using nonlocal keyword

In the scenario where you want to access the variable defined in outer function with in the nested function you can use nonlocal keyword in Python to refer to previously bound variables in the nearest enclosing scope.

def myfunction():
  x = 7
  def infunction():
    nonlocal x
    x = 20
    print('x inside infunction', x)

  infunction()
  print('x inside myfunction', x)

myfunction()

Output

x inside infunction 20
x inside myfunction 20

As you can see now nested function is accessing the x defined in the outer scope.

Here note that though nonlocal keyword is used to refer to previously bound variables in the nearest enclosing scope, but it does exclude global.

x = 10
def myfunction():
  x = 7
  def infunction():
    nonlocal x
    x = 20
    print('x inside infunction', x)

  infunction()
  print('x inside myfunction', x)

myfunction()
print ('x in global scope', x)

Output

x inside infunction 20
x inside myfunction 20
x in global scope 10

That's all for this topic Nonlocal Keyword in Python With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Namespace And Variable Scope in Python
  2. Name Mangling in Python
  3. Magic Methods in Python With Examples
  4. Removing Spaces From String in Python
  5. Interface in Python

You may also like-

  1. Class And Object in Python
  2. Abstract Class in Python
  3. Concatenating Lists in Python
  4. Python Program to Count Occurrences of Each Character in a String
  5. Linear Search (Sequential Search) Java Program
  6. Selection Sort Program in Java
  7. Difference Between Encapsulation And Abstraction in Java
  8. How MapReduce Works in Hadoop