简体   繁体   中英

How to use local, non-local and global variables in the same inner function without errors in Python?

When trying to use the local and non-local variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        x = 10 # Local variable
        x += 1
        print(x)
        
        nonlocal x # Non-local variable
        x += 1
        print(x)
    inner()
outer()

Or, when trying to use the global and non-local variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        global x # Global variable
        x += 1
        print(x)
        
        nonlocal x # Non-local variable
        x += 1
        print(x)
    inner()
outer()

I got the error below:

SyntaxError: name 'x' is used prior to nonlocal declaration

And, when trying to use the local and global variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():        
        x = 10 # Local variable
        x += 1
        print(x)
        
        global x # Global variable
        x += 1
        print(x)
    inner()
outer()

Or, when trying to use the non-local and global variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        nonlocal x # Non-local variable
        x += 1
        print(x)
        
        global x # Global variable
        x += 1
        print(x)
    inner()
outer()

I got the error below:

SyntaxError: name 'x' is used prior to global declaration

In addition, when trying to define the non-local and global variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        nonlocal x # Non-local variable
        global x # Global variable
    inner()
outer()

Or, when trying to define the global and non-local variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        global x # Global variable
        nonlocal x # Non-local variable
    inner()
outer()

I got the error below:

SyntaxError: name 'x' is nonlocal and global

And, when trying to define the local and non-local variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        x = 10 # Local variable
        nonlocal x # Non-local variable
    inner()
outer()

I got the error below:

SyntaxError: name 'x' is assigned to before nonlocal declaration

And, when trying to define the local and global variables x in inner() as shown below:

x = 0
def outer():
    x = 5
    def inner():
        x = 10 # Local variable
        global x # Global variable
    inner()
outer()

I got the error below:

SyntaxError: name 'x' is assigned to before global declaration

So, how can I use local, non-local and global variables in the same inner function without the errors above?

You can't do this. What you can do is avoid creating local names that shadow non-local names when you know you want to use the non-local ones.

x = 0
def outer():
    y = 5  # Not x; inner wants to use the global x
    def inner():
        global x
        x += 1
        print(x)
        
        nonlocal y
        y += 1
        print(y)
    inner()
outer()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM