UnboundLocalError

The error raised when a function reads a local name before anything has been assigned to it.

In more detail

Assigning to a name anywhere in a function makes it local for the whole function, so x += 1 inside a function fails when x exists only at module level. A global statement, or a parameter, fixes it. It is a subclass of NameError.

Example

x = 1
def f():
    x += 1  # UnboundLocalError

Related terms

Taught in

References

Certification courses