What is the difference between non local variable and global variable?
Asked Answered
E

5

24

I'm learning the concepts of programming languages.

I found the terminology "nonlocal" in python syntax.

What is the meaning of nonlocal in python?

Ethelda answered 19/10, 2015 at 9:44 Comment(3)
See smallsurething.com/a-quick-guide-to-nonlocal-in-python-3Masson
Does this answer your question? Python nonlocal statement / keywordLineup
also they are generally considered poor style.Kolivas
S
31

The nonlocal variables are present in a nested block. A keyword nonlocal is used and the value from the nearest enclosing block is taken. For example:

def outer():
    x = "local"
    
    def inner():
        nonlocal x
        x = "nonlocal"
        print("inner:", x)
    
    inner()
    print("outer:", x)

The output will be "nonlocal" both the times as the value of x has been changed by the inner function.

Sekyere answered 31/10, 2019 at 9:53 Comment(1)
Don't you mean "block" or "scope" rather than "loop"? There are no loops in this example.Veach
O
22

"nonlocal" means that a variable is "neither local or global", i.e, the variable is from an enclosing namespace (typically from an outer function of a nested function).

An important difference between nonlocal and global is that a nonlocal variable must have been already bound in the enclosing namespace (otherwise an syntaxError will be raised) while a global declaration in a local scope does not require the variable is pre-bound (it will create a new binding in the global namespace if the variable is not pre-bound).

Otolith answered 15/4, 2022 at 0:44 Comment(0)
S
20

nonlocal foo in inner() can access the non-local variable foo = 10 in middle() but not the non-local variable foo = 5 in outer() or the global variable foo = 0 outside outer() as shown below:

foo = 0 # <- ✖
def outer():
    foo = 5 # <- ✖
    def middle():
        foo = 10 # <- 〇
        def inner():
            nonlocal foo # Here
            foo += 1
            print(foo) # 11
        inner()
    middle()
outer()

global foo in inner() can access the global variable foo = 0 outside outer() but not the non-local variable foo = 5 in outer() and middle() as shown below:

foo = 0 # <- 〇
def outer():
    foo = 5 # <- ✖
    def middle():
        foo = 10 # <- ✖
        def inner():
            global foo # Here
            foo += 1
            print(foo) # 1
        inner()
    middle()
outer()
Sloatman answered 8/1, 2023 at 20:26 Comment(0)
A
6

From the documentation about nonlocal statements:

The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.

Names listed in a nonlocal statement, unlike to those listed in a global statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously).

Names listed in a nonlocal statement must not collide with pre- existing bindings in the local scope

Aggie answered 19/10, 2015 at 9:54 Comment(0)
B
-1
global x

def outer():

    x="global"

    def inner():
        nonlocal x
        x="nonlocal"

    def inner2():
        x="local"

inner2()

print(x)

inner()

print(x)

outer()

Output:

global

nonlocal
Below answered 8/11, 2023 at 0:59 Comment(1)
The sample given is not correct python code.Andrews

© 2022 - 2025 — McMap. All rights reserved.