Learning summary of global variables and local variables of python functions

1. Definition of global variables and local variables:

A global variable is a variable that can be called in the entire scope. A
local variable is a variable that can be called within a function, but cannot be called once the function comes to the whole . We call it a local variable. Only a function can be included in its own body. 'something 'localized' for example:


name = 'capture' #The name here is a global variable
def test():
	name = 'Liang Extraordinary' #And the name here is a local variable
	print(name)
test()
print(name) #The name output here is capture, although Liang Feifei has been assigned to name in the function, but because it is a local variable, it will be invalid when the function is exited		

2. How to change a local variable to a global variable?

In the above example, we add a sentence before name = 'Liang Feihan'

global name

In this way, the local variable name can be turned into a global variable. The global
here just gives a channel, so that the function can be linked to the global variable followed by global through this channel
. Things inside the function generally want to call parameters or When doing other things, you will first look for it from inside the function, and you will find the overall situation when you can’t find it (just like when you want to eat at home, you must first look for food at home, and then go out to find food if you can’t find it).
For example:
def test1():
	global name
	print(name)
def test2():
	name = '123'
	print(name)
test1()
test2 ()
        Like here, although test1() has referenced the name as a global variable, in test2(), the name = '123' of the local variable is given priority according to the basic method.
Finally, if it is used in the nested function of the function The global keyword, python will look for the outermost global variable, not the variable of the upper-level function.
If you want to call the variable of the upper-level function, please use the keyword: nonlocal 3. Suggestion : When declaring variables, the most It's good to give the variable names of global global variables something different relative to the variable names of local variables, and make them different names, so that it is easier to distinguish local variables from global variables




4. Execution order of nested functions

Nested functions are actually executed according to the basic method of python, that is, from front to back, according to indentation, but it is worth mentioning because it is relatively complicated.
For example :
x = 'This is the 0th string' #1
def a1():                               #2
    x = 'this is the first string' #3
    print(x)                            #4
    def a2():                           #5
        x = 'this is the second string' #6
        print(x)                        #7
        def a3(): #8
            x = 'This is the third string' #9
            print(x)                    #10
        a3 () # 11
    a2() #12
a1 () # 13
print(x)                                #14
The execution order here should be 1-13-3-4-12-6-7-11-9-10-14

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325777308&siteId=291194637