📜  UnboundLocalError:局部变量 - Python (1)

📅  最后修改于: 2023-12-03 15:20:49.193000             🧑  作者: Mango

UnboundLocalError: local variable - Python

Introduction

In Python, the UnboundLocalError is a common error that occurs when a local variable is referenced before it has been assigned a value in the current scope. This error can be frustrating to debug, as the code may compile without errors but still produce unexpected results when run.

There are a few common scenarios where this error is likely to occur, such as when using nested functions or when trying to modify a variable declared outside of a function from within that function.

Causes

The primary cause of an UnboundLocalError is attempting to access a local variable before it has been given a value in the current scope:

def my_function():
    print(my_variable) # Will raise UnboundLocalError
    my_variable = 42

In this example, we are attempting to access my_variable before it has been assigned a value. This will result in an UnboundLocalError being raised.

Another common scenario where this error can occur is in nested functions. When a variable is used in a nested function, Python will search for the variable in the outermost scope. However, if the variable is then assigned a value within the nested function, Python will treat it as a local variable and throw an UnboundLocalError if it is accessed before being assigned:

def outer_function():
    x = 1

    def inner_function():
        print(x) # Will raise UnboundLocalError
        x = 2

    inner_function()

In this example, x is assigned a value within the nested function inner_function(). Therefore, when we attempt to access it before it has been assigned a value, Python will throw an UnboundLocalError.

Solutions

To solve an UnboundLocalError, you need to make sure that any local variables are assigned a value before they are used. This can be achieved in a few different ways, depending on the scenario in which the error occurs.

If the error is occurring in a nested function, you can use the nonlocal keyword to indicate that the variable is not local to the current function:

def outer_function():
    x = 1

    def inner_function():
        nonlocal x
        print(x) # Will output 1
        x = 2

    inner_function()

In this example, the nonlocal keyword is used to indicate that x is not local to inner_function(). This allows us to access and modify x without running into an UnboundLocalError.

Alternatively, if the error is occurring outside of a function, you can ensure that any local variables are assigned a value before they are used:

my_variable = None

def my_function():
    print(my_variable) # Will output None
    my_variable = 42

In this example, my_variable is assigned a value of None outside of the function. This allows us to access it within the function without running into an UnboundLocalError. We can then assign it a value within the function as needed.

Conclusion

The UnboundLocalError is a common error that occurs in Python when a local variable is referenced before it has been assigned a value in the current scope. To avoid this error, make sure that any local variables are assigned a value before they are used. This can be achieved by using the nonlocal keyword in nested functions, or by assigning a default value to the variable outside of the function.