Python does not return _ local variables and global variables; python does not return
Local and global variables
Local variable: The variable defined in the function. The scope is the current function and only applies to the current function.
Global variable: a variable defined at the beginning of the Code. The scope is the whole code and it takes effect for the whole code.
name = 'jwc'def func1(name): print('my name is %s' %(name)) name = 'gally' print('now,my name is %s' %(name))func1('jiang')print(name)
Output result:
My name is jiang
Now, my name is gally
Jwc
According to the results of function running, when global variables are the same as local variables, local variables are used in the function and global variables are used outside the function.
If we want to make the local variables affect global variables within the function, we can use gloabl in the function. Let's take a look.
name = 'jwc'def func2(): global name name = 'gally' print(name)func2()print(name)
Output result:
Gally
Gally
Let's take a look.
name_info =['jack','jiang','jwc']def func3(): name_info[0] = 'gally'func3() print(name_info)
Output result:
['Gally ', 'jiang', 'jwc ']
Here we can see that the global variable name_info has changed, but the global variable has not been called in the function. In python, if you only modify the value of an element, such as a list or dictionary, you do not need gloabl. If you want to modify the entire list, you must have gloabl.
name_info=['jack','jiang','jwc']def func4(): name_info = []func4()print(name_info)
Output result:
['Jack', 'jiang ', 'jwc']
name_info=['jack','jiang','jwc']def func5(): gloabl name_info name_info = []func5()print(name_info)
Output result:
[]
Summary: A local function only acts on this function. A global function acts on the entire code segment. to modify the value of a global variable in the function, use gloabl. If you only modify a value in the list or dictionary, you can modify the global variable without using global.
------- O & M that won't be developed is not a good cook