如何在Python中在同一个内部函数中使用局部、非局部和全局变量而不出错?

huangapple go评论81阅读模式
英文:

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:

  1. 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()
  1. 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()
  1. 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()

huangapple
  • 本文由 发表于 2023年1月8日 22:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定