Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

Nonlocal Keyword in Python With Examples

$
0
0

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

To understand global 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 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. Class And Object in Python
  3. Abstract Class in Python
  4. Name Mangling in Python
  5. Interface in Python

You may also like -

  1. Linear Search (Sequential Search) Java Program
  2. Selection Sort Program in Java
  3. Difference Between Encapsulation And Abstraction in Java
  4. What Are JVM, JRE And JDK in Java
  5. Map Operation in Java Stream API
  6. Batch Processing in Java JDBC - Insert, Update Queries as a Batch
  7. Spring Web MVC Tutorial
  8. How MapReduce Works in Hadoop

Viewing all articles
Browse latest Browse all 938

Trending Articles