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 global variables and global keywords
- Python error: local variable ‘XXXX’ referenced before assignment
- Solving Python error: local variable ‘a’ referenced before assignment
- From in Python__ future__ The role of import *
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Tensorflow in function tf.Print Method of outputting intermediate value
- Problem: attributeerror: ‘tensor’ object has no attribute ‘creator’
- Ioerror: [errno 13] permission denied
- Methods of modifying index and columns names by dataframe in pandas
- Time, strftime and strptime in Python
- Detailed explanation of yield in Python — the simplest and clearest explanation
- Python: Understanding__ str__
- In Python, print() prints to remove line breaks
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- After upgrading php7, PHP program prompts an error: operator not supported for strings in causes and Solutions
- Raspberry pie 4B uses adafruit_ Pca9685 report error ioerror: [errno 121] remote I / O error solution
- Raspberry pie upgrade to Python 3.7.3
- Go start error: Panic: runtime error: invalid memory address or nil pointer reference
- NPM- Cannot find module ‘xxxxx’
- Differences between shell script variable assignment and C language variable assignment