Conclusion:
- internal function, do not change the global variable can access the global variable internal function, modify the same global variable, python will think it is a local variable before internal function to modify a global variable with the same call variable names (such as print the sum), cause Unbound – LocalError
ol>
The sum set in the program belongs to the global variable, and there is no definition of sum in the function. According to Python’s rules for accessing local and global variables, when searching for a variable,
If the variable is not found in the local scope, then Python looks for the variable in the global variable. If it is not found, it throws an exception (NameError or Unbound-LocalError, depending on the Python version).
if you have any references to external internal function function of the same variable or a global variable, and is modified for this variable. Python assumes that it is a local variable, and because there is no definition or assignment of sum in the function, it returns an error.
from the following two procedures separate access or modify a global variable, is not an error ~
# accessing global variables #! /usr/bin/python
import
sys
sum
=
5
def
add
(
a
=
1
.
b
=
3
) :
print
a
.
b
print
sum
#
Just to visit
add
(
4
.
8
)
print
sum
[
root
@rac3
python
]
# python local.py
4
8
5
5
# modify a global variable of the same name
Is considered a local variable
#! /usr/bin/python
import
sys
sum
=
5
def
add
(
a
=
1
.
b
=
3
) :
print
a
.
b
#
An inner function has a variable of the same name or a global variable that refers to an outer function, and changes are made to that variable. Python will think of it as a local variable
sum
=
b
+
a
#
Modify inside the function
print
sum
add
(
4
.
8
)
[
root
@rac3
python
]
# python local.py
4 8
12
If the inner function has a variable of the same name or a global variable that refers to the outer function, and the variable is modified. Python assumes that it is a local variable, and because there is no definition or assignment of sum in the function, it returns an error
#! /usr/bin/python
import
sys
sum
=
5
def
add
(
a
=
1
.
b
=
3
) :
print
a
.
b
print
The sum # inner function refers to a variable of the same name and modifies that variable. Python thinks of it as a local variable. Because the sum variable was not defined before print here, an error will be reported. (It is recommended to compare with case 1, note: this is only before the above example: print sum)
sum
=
b
+
a
print
sum
add
(
4
.
8
)
print
sum
[
root
@rac3
python
]
# python local.py
4
8
Traceback
(
most
recent
call
last
) :
File
“local.py”
.
line
10
.
in
?
add
(
4
.
8
)
File
“local.py”
.
line
6
.
in
add
print
sum
UnboundLocalError
:
local
variable
‘sum’
referenced
before
assignment
You can use the: global keyword when you access a global variable in your program and you want to change the value of the global variable. Declare the variable as a global in a function
#! /usr/bin/python
import
sys
sum
=
5
print
‘Before changing: sum=’
.
sum
def
add
(
a
=
1
.
b
=
3
) :
global
sum
print
‘add ‘:sum=’
.
sum
sum
=
b
+
a
print
‘function :sum=’
.
sum
add
(
4
.
8
)
print
‘change sum=’
.
sum
[
root
@rac3
python
]
# vim local.py
Before you change:
sum
=
5
add
In the function
:
sum
=
5
When I change the function
:
sum
=
12
After the change
sum
=
12
Read More:
- Python UnboundLocalError: local variable ‘num’ referenced before assignment
- Solving Python error: local variable ‘a’ referenced before assignment
- Python error: local variable ‘XXXX’ referenced before assignment
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Differences between shell script variable assignment and C language variable assignment
- Node configuration environment variable and global installation of webapck
- non-static variable this cannot be referenced from a static context
- Python global variables and global keywords
- Error: transfer of control bypasses initialization of: variable XXX solution
- May cause (ora-01006: binding variable does not exist) problems
- Set path environment variable through cmd
- You set the variable “no_check_targets“ here and it was unused before it went out of scope.
- ERROR: The environment variable VG_GNU_PACKAGE must be set. Aborting.
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- variable ‘std:ofstream’ has initializer but incomplete type
- Shell script: syntax error: bad for loop variable error resolution
- Unity “Feature `out variable declaration’ cannot be used because it is not part of the C# 4.0” error
- PHP reports “parse error: syntax error, unexpected T”_ The solution of “variable”
- Index error: invalid index to scalar variable
- pg_ctl: no database directory specified and environment variable PGDATA unset , centos 7 postgreSQL