Easy Tutorial
❮ Verilog2 Real2Int Android Tutorial Activity ❯

Global Variable Error: UnboundLocalError: local variable 'l' referenced before assignment

>

Summary:

In the program, the sum set belongs to the global variable, and there is no definition of sum in the function. According to the rules of Python's access to local and global variables: when searching for a variable, Python starts from the local scope, if it doesn't find the variable in the local scope, then Python looks for it in the global variables, if it still can't find it, an exception is thrown (NAMEERROR or Unbound-LocalError, depending on the Python version).

If the inner function references the same name variable of the external function or global variable and modifies this variable, then Python will consider it a local variable, and since there is no definition and assignment of sum in the function, an error will occur.

From the following two programs, it can be seen that accessing or modifying global variables alone does not cause an error.

Accessing the global variable:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

sum=5
def add(a=1,b=3):
    print a,b
    print sum   # Just access
add(4,8)
print sum

Output result:

4 8
5
5

Modifying the same name global variable is considered a local variable:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys
sum=5
def add(a=1,b=3):
    print a,b
    # The inner function references the same name variable as the external function or global variable, and modifies this variable. Python will consider it a local variable.
    sum=b+a # Modify inside the function
    print sum
add(4,8)

Output result:

4 8
12

The following program will cause an error because "If the inner function references the same name variable as the external function or global variable, and modifies this variable, Python will consider it a local variable, and since there is no definition and assignment of sum in the function, an error will occur:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys
sum=5
def add(a=1,b=3):
    print a,b
    print sum  # The inner function references the same name variable and modifies this variable. Python will consider it a local variable. Because there is no definition of the sum variable before this print, an error will occur (compare with case one, note: just print sum before the previous example)
    sum=b+a
    print sum
add(4,8)
print sum

Error message:

4 8
Traceback (most recent call last):
...
...
...
UnboundLocalError: local variable 'sum' referenced before assignment

If you encounter a situation in the program where you need to access and modify the value of a global variable, you can use the global keyword to declare this variable as a global variable in the function.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

sum=5
print 'Before change: sum=',sum
def add(a=1,b=3):
    global sum
    print 'In add function: sum=',sum
    sum=b+a
    print 'After change in function: sum= ',sum
add(4,8)
print 'After change sum=',sum

Output result:

Before change: sum= 5
In add function: sum= 5
After change in function: sum=  12
After change sum= 12

>

Original link: https://blog.csdn.net/my2010sam/article/details/17735159

** Click to share notes

Cancel

-

-

-

❮ Verilog2 Real2Int Android Tutorial Activity ❯