When you define variables inside a function, they have nothing to do with other variables that have the same name outside the function, that is, the variable name is local to the function, which is called the scope of the variable, as an example:
def func_local (x): print ' x is ', x x = 2 "print ' chanaged local x to ', XX = 50func_local (x)" print ' x is still ', X
Execution Result:
X is 50Chanaged local x to 2x is still 50
If you want to change the value of a variable outside of a function, use the global statement to complete
Def func_global (): global y print ' y is ', y y = [print ' Changed local y to ', yy = 10func_global () print ' Value of Y is ', y
Def func_global (): global y print ' y is ', y y = [print ' Changed local y to ', yy = 10func_global () print ' Value of Y is ', y
Execution Result:
Y is 10Changed local y to 50Value of y is 50
Y is 10Changed local y to 50Value of y is 50
If the function parameter is a list, set, dict variable parameter, changing the parameter within the function causes the parameter to change, for example:
def func_local (x): print ' x is ', x x.append (Ten) print ' chanaged local x to ', XX = range (6) func_local (x) print ' X is ', X
def func_local (x): print ' x is ', x x.append (Ten) print ' chanaged local x to ', XX = range (6) func_local (x) print ' x is ', X
Execution results
X is [0, 1, 2, 3, 4, 5]chanaged local x to [0, 1, 2, 3, 4, 5, 10]x are [0, 1, 2, 3, 4, 5, 10]
X is [0, 1, 2, 3, 4, 5]chanaged local x to [0, 1, 2, 3, 4, 5, 10]x are [0, 1, 2, 3, 4, 5, 10]
def func_local (x): print ' x is ', x x.add (Ten) print ' chanaged local x to ', XX = set (range (6)) func_local (x) print ' x is ', X
def func_local (x): print ' x is ', x x.add (Ten) print ' chanaged local x to ', XX = set (range (6)) func_local (x) print ' x is ', X
Execution Result:
X is set ([0, 1, 2, 3, 4, 5]) chanaged local x to set ([0, 1, 2, 3, 4, 5, ten]) x is set ([0, 1, 2, 3, 4, 5, 10])
X is set ([0, 1, 2, 3, 4, 5]) chanaged local x to set ([0, 1, 2, 3, 4, 5, ten]) x is set ([0, 1, 2, 3, 4, 5, 10])
def func_local (x): print ' x is ', x x[' x '] = 2 print ' chanaged local x to ', XX = Dict ([' X ', 1 '), (' Y ', 2)]) func_l Ocal (x) print ' x is ', X
def func_local (x): print ' x is ', x x[' x '] = 2 print ' chanaged local x to ', XX = Dict ([' X ', 1 '), (' Y ', 2)]) func_l Ocal (x) print ' x is ', X
Execution Result:
X is {' Y ': 2, ' x ': 1}chanaged local x to {' Y ': 2, ' x ': 2}x is {' Y ': 2, ' X ': 2}
X is {' Y ': 2, ' x ': 1}chanaged local x to {' Y ': 2, ' x ': 2}x is {' Y ': 2, ' X ': 2}
def func_local (x): print ' x is ', x x = (4, 5, 6) print ' chanaged local x to ', XX = (all in A,) func_local (x) print ' X is ', X
def func_local (x): print ' x is ', x x = (4, 5, 6) print ' chanaged local x to ', XX = (all in A,) func_local (x) print ' X is ', X
Execution results
X is (1, 2, 3) chanaged local x to (4, 5, 6) x is (1, 2, 3)
X is (1, 2, 3) chanaged local x to (4, 5, 6) x is (1, 2, 3)
If a variable parameter such as list, set, Dict is passed, the parameter is changed inside the function, the parameter itself changes, and the tuple and Str are unchanged.
Python-global Global Variables