英文:
How to use local, non-local and global variables in the same inner function without errors in Python?
问题
You can use local, non-local, and global variables in the same inner function by following these guidelines to avoid the errors you mentioned:
- Local and Non-local Variables in the Same Inner Function:
x = 0
def outer():
x = 5
def inner():
nonlocal x # Declare x as non-local first
x = 10 # Modify the non-local variable
x += 1
print(x)
inner()
outer()
- Global and Non-local Variables in the Same Inner Function:
x = 0
def outer():
x = 5
def inner():
global x # Declare x as global first
nonlocal x # Then declare it as non-local
x += 1
print(x)
inner()
outer()
- Local, Non-local, and Global Variables in the Same Inner Function (non-local and global should not both modify the same variable 'x'):
x = 0
def outer():
x = 5
def inner():
x = 10 # Local variable
nonlocal x # Declare x as non-local
x += 1 # Modify non-local 'x'
print(x)
global y # Declare another variable 'y' as global
y = 20 # Modify global 'y'
print(y)
inner()
outer()
Note that in the third example, there are two separate variables, 'x' and 'y,' where 'x' is first declared as non-local and 'y' is declared as global. Each variable is modified within its respective scope without conflicting with each other.
英文:
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?
答案1
得分: 1
你不能这样做。你可以做的是,在你知道要使用非本地名称时,避免创建与非本地名称重名的本地名称。
x = 0
def outer():
y = 5 # 不是 x;内部想要使用全局的 x
def inner():
global x
x += 1
print(x)
nonlocal y
y += 1
print(y)
inner()
outer()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论