-
global global variables
- global is the global variable, variables to be changed when the function table function outside when needed to represent global global variable
global use
For list type: Change the first and last letters
newName = "xiaoming" lst4 = list(newName) def change1(): lst4 = ['g', 'i', 'a', 'o', 'm', 'i', 'h', 'x'] change1() print(lst4) def change2(): global lst4 lst4 = ['g', 'i', 'a', 'o', 'm', 'i', 'h', 'x'] change2() print(lst4) 结果分别为: ['x', 'i', 'a', 'o', 'm', 'i', 'h', 'g'] ['g', 'i', 'a', 'o', 'm', 'i', 'h', 'x']
For string type:
name3 = "xiaoming" def change3(): name3 = "giaominx" change3() print(name3) def change4(): global name3 name3 = "giaominx" change4() print(name3) 结果: xiaoming giaominx
For int type:
i = 3 def increase(): global i i = 4 increase() print(i) 结果: 4
Summary: 1, need to declare the function as a global variable when you want to change the function of external variables in the function global
2, change1 and change3 in lst4, name3 in fact, is not a function variables defined outside the function is a new function redefined
Equivalent remaining methods
lst3 = list(newName) def swap1(): h = lst3[0] lst3[0] = lst3[-1] lst3[-1] = h swap1() print(lst3)
Equivalent to pass arguments to realize:
lst3 = list(newName) def swap2(lst): h = lst[0] lst[0] = lst[-1] lst[-1] = h swap2(lst3) print(lst3)
But not only change the function and gave the variable value reassignments:
lst3 = list(newName) def swap3(): h = lst3[0] lst3[0] = lst3[-1] lst3[-1] = h lst3 = ['g', 'i', 'a', 'o', 'm', 'i', 'h', 'x'] swap3() print(lst3) 报错:UnboundLocalError: local variable 'lst3' referenced before assignment
If you want to change the value of the assignment and operation can be global
lst3 = list(name3) def swap1(): global lst3 h = lst3[0] lst3[0] = lst3[-1] lst3[-1] = h lst3 = ['x', 'i', 'a', 'o'] swap1() print(lst3)
string data type can not be so, because the string data can not be changed
def swap4(): h = newName[0] newName[0] = newName[-1] newName[-1] = h swap4() print(newName) TypeError: 'str' object does not support item assignment
to sum up:
-
When the data type is a reference data type, the parameters can be changed by passing the actual input parameter swap2
-
When the data is the basic data type definition of the global time needed to change the value of the data
-
Java is defined in the variable outside the function is a member variable, its value can be changed directly in the function, you should not want to change you need to define a variable name different names
-
this is the difference between the global and 1, in Python global refers to global variables, expressed only after specify the same variable (special cases: when the variable is a reference data type, change its value when the same variable may be expressed as swap1) , when the external variable to be changed when using the 2, in java this refers to the member variable, has no need to specify the same variable as represented, when the variables and member functions of the same name as a variable in order to distinguish the use of 3, comparison , java greater flexibility, but easy to tamper with the data values in the function, so the definition of member variables need to be careful not to worry Python named the same, but the change is more complex global variables