function - namespace

   

Namespace classification: global namespace, local namespace, built-in namespace.

    Let's first recall how functions are encountered when Python code is running.

       After the Python interpreter starts executing, a space is opened up in memory. Whenever a variable is encountered, the corresponding relationship between the variable name and the value is recorded, but when a function definition is encountered, the interpreter will It just symbolically reads the function name into memory, indicating that the function exists. As for the variables and logic inside the function, the interpreter doesn't care at all.

   When the function call is executed, the Python interpreter will open up another piece of memory to store the contents of the function. At this time, only pay attention to the variables in the function, and the variables in the function will be stored in the newly opened memory. The variables in the function can only be used inside the function, and when the function is executed, all the contents of this memory will be cleared.

     The space that 'stores the relationship between names and values' is called a namespace.

      When the code starts running, the space created to store the "relationship between variable names and values" is called the global namespace;

      The temporary space opened up during the operation of the function is called the local namespace, also known as the temporary namespace. The relationship between variables and values ​​stored in a function is temporarily opened with the execution of the function, the execution of the function ends, and the temporary namespace disappears.

  The built-in namespace stores the names provided by the python interpreter: input, print, str, list, tuple, len... They are all available for use. 

# name = 'alex'
# age = 12
# def func1():
# name1 = 'wusir'
# age1 = 34
# func1()
# print(name1)

scope

   The scope is the scope, which can be divided into global scope and local scope according to the effective scope.

   Global scope: Contains built-in namespaces, global namespaces , can be referenced anywhere in the entire file, and is globally valid

   local scope: local namespace, which can only take effect in the local scope



#Load order and value order.
#Loading order: built-in namespace (loaded before the program runs) -> global namespace (while the program is running: loaded from top to bottom) -> local namespace (while the program is running: loaded when called)
#Value order:      

  Called globally: global namespace -> built-in namespace

   Called locally: local namespace -> global namespace -> built-in namespace

 

Local namespaces can reference global variable values, but cannot change them.

global keyword, nonlocal keyword.

global:

  1. Declare a global variable.

  2. When you want to modify the global variables of the global scope in the local scope, you need to use global (limited to strings, numbers).

def func():
    global a can be found in the global scope
    a = 3
func()
print(a)


count = 1
def search():
    global count
    count = 2
search()
print(count)
copy code
def func():
    global to
    a = 3
func()
print(a)


count = 1
def search():
    global count
    count = 2
search()
print(count)
copy code

 ps: Variable data types (list, dict, set) can be directly referenced without going through global.

li = [1,2,3]
dic = {'a':'b'}

def change():
    li.append('a')
    dic['q'] = 'g'
    print (Dec)
    print (li)
change()
print (li)
print (Dec)

copy code
li = [1,2,3]
dic = {'a':'b'}

def change():
    li.append('a')
    dic['q'] = 'g'
    print (Dec)
    print (li)
change()
print (li)
print (Dec)
copy code

nonlocal:

  1. Global variables cannot be manipulated.

  2. In the local scope, refer to and modify the variables of the parent scope (or the outer scope non-global scope), and all the variables from the referenced layer and below are changed.

def func1(): 
name1 = 'xyn'
print('aaa',name1)
def inner(): nonlocal
name1
name1 = 'wusir'
print('###',name1)
inner()
print('bbb',name1 )
func1()

running result:

aaa xyn
### wusir
bbb wusir

 

def add_b():
    b = 42
    def do_global():
        b = 10
        print(b)
        def dd_nonlocal():
            nonlocal b
            b = b + 20
            print(b)
        dd_nonlocal()
        print(b)
    do_global()
    print(b)
The result of add_b() 
operation:

10
30
30
42

copy code
def add_b():
    b = 42
    def do_global():
        b = 10
        print(b)
        def dd_nonlocal():
            nonlocal b
            b = b + 20
            print(b)
        dd_nonlocal()
        print(b)
    do_global()
    print(b)
add_b()

Guess you like

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