| by Arround The Web | No comments

Python Nonlocal Keyword

In Python, two variable types, “local” and “global”, are declared/initialized inside the function. The “local” variables are declared within the nested function, and the “global” variables are declared outside the nested function. The “nonlocal” keyword, however, is used inside the nested function to declare the variable belonging to a local variable.

This article will present you with a thorough guide on Python “nonlocal” keyword using various examples and the following supported content:

What is the Python Nonlocal Keyword?

In Python, the “nonlocal” keyword specifies that the particular variable is not part of the inner or nested function and that the particular variable is not local. This keyword is utilized to deal with variables inside the nested function. There is no way to link to a global or local variable through the “nonlocal” keyword. It can only be utilized inside/within nested structures.

Example 1: Using the “nonlocal” Keyword

The following code indicates the use of the “nonlocal” keyword within the nested function:

def team():
  name = "Joseph"
  def points():
    nonlocal name
    name = 45
  points()
  return name
print(team())

 
In the above code:

    • The function named “team()” is defined, and the variable “name” is initialized, respectively.
    • The nested function “points()” is defined, and the “nonlocal” keyword is used to indicate that the variable value does not belong to the nested function.
    • The “name” variable value is then overwritten with a new value.
    • The main function is accessed, and the overwritten value of the “nonlocal” variable is displayed on the console.

Output


After making the variable “nonlocal”, the function returned the variable “name” value as “45”, overwritten in the nested function.

Example 2: Without Using the “nonlocal” Keyword

The following code does not use the “nonlocal” keyword. Instead, it uses the global variable within the nested function:

def team():
  name = "Joseph"
  def points():
    name = 45
  points()
  return name
print(team())

 
According to the above code:

    • The function named “team()” and nested function “points()” are defined, respectively.
    • The variable “name” is overwritten in the latter function with the new value “45”.
    • The main function, “team()” is accessed and returns the value.
    • The returned value here contains the old/former variable value because the overwritten variable value is considered a local variable.

Output


The variable value “Joseph” implies that the new overwritten variable value is local to the nested function.

Conclusion

A variable should not belong to the inner function when it is declared as “nonlocal ” inside nested functions. It allows the nested function to modify the variable value that is specified in the outer function. This blog delivered a tutorial on Python’s “nonlocal” keyword using relevant examples.

Share Button

Source: linuxhint.com

Leave a Reply