Global variable error: unboundlocalerror:local variable ' l ' referenced before assignment

Source: Internet
Author: User
Tags throw exception
Summarize:
Internal functions, you can access the global variable internal functions, modify global variables, and the same name, Python will think that it is a local variable called the variable name (such as print sum) before the internal function modifies the global variable with the same name, and then throws the Unbound-localerror
The sum set in the program is a global variable, and there is no definition of sum in the function, and rules for accessing local and global variables are based on Python: When searching for a variable, Python first searches from the local scope, and if the variable is not found in the local scope, So python looks for this variable in the global variable, and if you can't find the throw exception (Nameerror or unbound-localerror, it depends on the Python version.) )

If the intrinsic function has a variable of the same name that refers to an external function, or a global variable, and there is a modification to the variable. Then Python thinks it is a local variable, and because the function does not have the definition and assignment of sum, the error occurs.

From the following two programs to see a separate access or modify the global variables, does not error ~

#访问全局变量
#!/usr/bin/python
Import SYS
sum = 5
def Add(A = 1, B = 3):
PrintA, b
PrintSum # just Access
Add ( 4, 8)
PrintSum
[Root @rac3Python] # python local.py
4 8
5
5

#修改同名的全局变量, it is considered a local variable
#!/usr/bin/python
Import SYS
sum = 5
def Add(A = 1, B = 3):
PrintA, b
# internal functions have a variable or global variable that refers to an external function, and there are modifications to this variable. Then Python would think it was a local variable
sum = B + A # modified inside function
PrintSum
Add ( 4, 8)
[Root @rac3Python] # python local.py
4 8
of
The following procedure is because "if the intrinsic function has a variable of the same name that refers to an external function, or a global variable, and the variable is modified." Then Python thinks it's a local variable, and because there's no definition and assignment of sum in the function, the error
#!/usr/bin/python
Import SYS
sum = 5
def Add(A = 1, B = 3):
PrintA, b
PrintSum #内部函数引用同名变量, and modify this variable. Python will think of it as a local variable. Because the sum variable is not defined before you print here, you will report an error (a comparison of the recommendations with the case, note: Here is only the first example of print sum)
sum = B + A
PrintSum
Add ( 4, 8)
PrintSum
[Root @rac3Python] # python local.py
4 8
Traceback (most recent call last):
File "local.py", line Ten,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.