-1

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?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • 4
    You can't. A variable can be only of one kind (scope) in a function. – Michael Butscher Jan 08 '23 at 14:24
  • 3
    You are in complete control of the names of the local variables; just don't reuse names of global and non-local variables you want to use locally. – chepner Jan 08 '23 at 14:25
  • 1
    Python wants you to use clear naming to make code very comprehensible. It doesn't give you the necessary tools to do what you're trying to do, which is for the better, because this is ultimately just shooting your own foot. – deceze Jan 08 '23 at 14:36
  • 1
    The only time this should be an issue is if you are doing complicated dynamic code generation or [lambda lifting](https://en.wikipedia.org/wiki/Lambda_lifting) in which case you probably need to be aware of variable renaming allowed by [alpha equivalence](https://en.wikipedia.org/wiki/Lambda_calculus#Alpha_equivalence). (If this doesn't make sense, that's OK. These aren't everyday programming techniques.) – chepner Jan 08 '23 at 14:41
  • You keep editing this question as if you're hoping for a different answer… There is none besides *use different variable names.* – deceze Jan 08 '23 at 19:49

1 Answers1

1

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()
chepner
  • 497,756
  • 71
  • 530
  • 681