Using global and nonlocal in Python


Modify variables outside of the current scope.

Source Code

x = 0
def outer():
    x = 1
    def inner():
        nonlocal x
        x = 2
        print("inner:", x)
    inner()
    print("outer:", x)

outer()
print("global:", x)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments