function namespace, scope

One: Why have namespaces?

The two functions find the maximum value, return the maximum value of the function, and print it out. Can it be printed directly? see the function below

eg1:

def max(x,y):
    z=x if x>y else y
return z    
bigger=max(16,20)
print(bigger)

Print result: 20

If so?

eg2:

def max(x,y):
    z=x if x>y else y
max(16,20)
print (z)

print result:

Why do I get an error?

Because we print, the global variable is printed, the internal variable z is not defined, and the global variable cannot refer to the internal variable.

That is:

When the python interpreter starts executing, a space is opened up in memory.

If a variable is not encountered, the corresponding relationship between the variable name and the value will be recorded.

But when it encounters a function definition, the python interpreter only symbolically reads the function into memory (meaning that the function already exists, as for the internal logic and variable interpreter does not care)

When the function is called, the python interpreter will open up another memory in the 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 the namespace

When the code starts running, the created relational space for storing names and values ​​is the global namespace, and the space temporarily opened during the function running process is the local namespace.

 

Two: The essence of namespace: storing the binding relationship between name and value

Zen of python:

Open the cmd command and enter import this in the python interpreter

Three: Types of namespaces

built-in namespace global namespace local namespace

Built-in namespace: store keywords provided by the python interpreter: more than 50 built-in keywords such as sum, print, str, list, etc.

Four: The loading and value order of the three namespaces

Load order:

Built-in (loaded before the program is running) ---- global (the program is running, loaded from top to bottom) ----- local (the program is running, the call is loaded)

Value order:

In local:

Partial---Global---Partial (Son can spend Lao Tzu's money, Lao Tzu can't spend his son's money)

a=1
def max():
    a=2
    return a
rr=max()
print(rr)
print(a)

Print result: 2,1

In the global: global --- local

x = 1
def f(x):
    print(x)
f(10)
print(x)

Print result: 10,1

Four: Scope

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

Global scope: including the global namespace and built-in namespace, the entire file can be referenced anywhere and takes effect globally.

Local scope: Only in the local namespace, the local scope is valid.

global 和 local

def func():
    a = 12
    b = 20
    print(locals())
    print (global ())
func()

print result

global: declares that the function is globally valid

a = 10
def func():
    global a
    a = 20
    print(a)
print(a)
func()
print(a)

his order of execution

Results of the:

Function nested calls:

def max2(x,y):
    m  = x if x>y else y
return m    

def max4(a,b,c,d):
    res1 = max2(a,b)
    res2 = max2(res1,c)
    res3 = max2(res2,d)
    return res3
print(max4(23,-7,31,11))
print(max2(10,20))

Show results:

 

Nested definitions of functions

eg1:

def f1():
    print("in f1")
    def f2():
        print("in f2")

    f2()
f1 ()

eg2:

def f1():
    def f2():
        def f3():
            print("in f3")

        print("in f2")
        f3()

    print("in f1")
    f2()
f1 ()

function scope chain

def f1():
    a = 1
    def f2():
        a = 2
    f2()
    print('a in f1 : ',a)
f1 ()

global is suitable for modifying the value of global variables inside the function

nonlocal is suitable for inner functions in nested functions to modify the value of outer variables

def f1():
    a = 1
    def f2():
        nonlocal a
        a = 2
    f2()
    print('a in f1 : ',a)
f1 ()

Print result: 2

'a in f1 : ',2

 

Guess you like

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