UnboundLocalError: local variable referenced before assignment
The "UnboundLocalError: local variable referenced before assignment" in Python occurs when you try to access a local variable before it has been assigned a value within the current scope or function. Python requires that you initialize a variable before using it in the same scope to avoid ambiguity and undefined behavior.
To resolve this error, you can follow these steps:
- Initialize the variable: Ensure that you assign a value to the variable before referencing it within the same scope.
- Check the variable scope: Verify that you are not trying to access the variable from outside its defined scope.
- Use global keyword : If you want to access a global variable from within a function, use the global keyword to indicate that the variable is defined in the global scope.
Variable not assigned before use
In this example, the variable "x" is referenced before it is assigned any value within the function. Python raises the UnboundLocalError since the variable is accessed before being defined.
To fix this, assign a value to the variable before referencing it:
Accessing global variable without 'global' keyword
In this example, the function tries to modify the global variable "x" without using the global keyword. This results in the UnboundLocalError.
To resolve this, use the global keyword to indicate that the variable is in the global scope:
In Python, lexical scoping is the default behavior, where inner scopes can access values from enclosing scopes, but they cannot modify those values unless explicitly declared global using the "global" keyword. When variables are assigned values within a function, they are stored in the local symbol table. However, when referencing variables, Python first looks in the local symbol table, then in the global symbol table, and finally in the built-in names table. This design leads to a restriction where global variables cannot be directly assigned new values within a function, unless they are explicitly named in a global statement. Nevertheless, global variables can be referenced within functions. By adhering to these lexical scoping principles and utilizing the "global" keyword when necessary, Python developers can ensure proper variable behavior and maintain the integrity of variable assignments across different scopes, promoting code reliability and clarity.
Conclusion
By following these best practices and ensuring variable assignment before referencing within the same scope, you can overcome the UnboundLocalError and ensure the proper functioning of your Python code.
- TypeError: 'NoneType' object is not subscriptable
- IndexError: string index out of range
- IndentationError: unexpected indent Error
- ValueError: too many values to unpack (expected 2)
- SyntaxError- EOL while scanning string literal
- TypeError: Can't convert 'int' object to str implicitly
- IndentationError: expected an indented block
- ValueError: invalid literal for int() with base 10
- IndexError: list index out of range : Python
- AttributeError: 'module' object has no attribute 'main'
- TypeError: string indices must be integers
- FileNotFoundError: [Errno 2] No such file or directory
- Fatal error: Python.h: No such file or directory
- ZeroDivisionError: division by zero
- ImportError: No module named requests | Python
- TypeError: 'NoneType' object is not iterable
- SyntaxError: unexpected EOF while parsing | Python
- zsh: command not found: python
- Unicodeescape codec can't decode bytes in position 2-3
- The TypeError: 'tuple' object does not support item assignment
- The AttributeError: 'bytes' object has no attribute 'read'