Function object / nesting / namespace / scope of Python syntax


1、 Function object

1. Function names can be referenced

def foo():
    print('from foo')
Res = foo ා the function name foo is given to res, and res and foo point to the same memory address
res()   #from foo

2. Function names can be passed as arguments

def foo1(a,b):  #a=1,b=foo2
    print(a)     #1
    B() ාb() is foo2() to execute the function of foo2(), result: from foo2
def foo2():
    print('from foo2')
Foo 1 (1, foo 2) ාfoo 2 is passed to B as a parameter

3. Function names can be used as return values

def foo(x):
    return x
def foo1():
    print('from foo1')
Res = foo (foo1) ා give function name foo1 to x, call foo() function to return foo1, and then give foo1 to res
Res() is foo1()

4. Function names can be used as container type elements

def func():
    print('from func')
l1 = [1,'2',func]
L = L1 [2], that is, l = func
l()       #func()

4.1 shopping cart:

func_dic = {
    '1': register,
    '2': login,
    '3': shopping,
    '4': pay

def main():
    while True:
            1. Registration
            2. Login
            3, shopping
            4, payment
            5, quit
        Choice = input ("please enter the corresponding number:"). Strip()
        if choice == '5':
        if choice not in func_dic:



2、 Nesting of functions

2.1 nested call of function: call function within function

2.2 nested definition of function: set function inside function


3、 Namespace

3.1 name classification

3.1.1. Built in namespace: Python gives a good name in advance, such as the built-in function name len and print.

3.1.2. Global namespace: the name stored at the file level and the name of the top cell.

3.1.3. Local namespace: the name defined within a function.


3.2 life cycle of namespace

3.2.1. Built in namespace: generated with the startup of Python interpreter and recycled with shutdown.

3.2.2. Global namespace: generated with the start of Python file execution and recycled after execution.

3.2.3 local namespace: generated along with function call, and recycled after function call.


3.3 order of namespaces

Loading order: built in namespace > global namespace > local namespace

Find order: local namespace > global namespace > built in namespace

#The name used inside the function is already specified in the definition phase, regardless of the call location

def func1():
    x = 2
    def func2():
        x = 3
        print('func2:', x)
    print('func1:', x)


4、 Scope

4.1, classification

4.1.1 global scope

Names in the global namespace and built-in namespace belong to the global scope. The names in this range are globally valid (can be used anywhere).

4.1.2 local scope

Names in the local namespace belong to the local scope. The names in this range are temporarily alive (that is, they are temporarily generated when the function is called and released when the function is called), locally valid (they can only be used within the function).


4.2 scope and name search priority

4.2.1 when searching for a name in the local scope, the starting position is the local scope, so first search for the local namespace, and then search for it in the global scope: first search for the global namespace, then search for the built-in namespace, and finally find no exception.

x = 1
def foo2():
    x = 2
    def foo3():
        x = 3
        print(x)  #x=1


4.2.2. When searching for a name in the global scope, the global scope starts at the starting position, so first look for the global namespace, then look for the built-in namespace, and finally find no exception.

x = 1
def foo2():
    x = 2
print(x)  #1
#4.2.3. Python supports the nested definition of functions. When finding the name of an embedded function, it will first find the name of its own local scope, and then look up the scope defined by the external nested function layer by layer from the inside. If none is found, it will find the global scope. X = 1
def foo():
    x = 2
    def foo1():
        x = 3
        def foo2():
            # x = 4
            def foo3():
                x = 5
                print('foo3:',x)  #5
            print('foo2:',x)  #4
        print('foo1:',x)  #3
    print(x)   #2

#No matter how many layers are nested in a function, you can view the name of the global scope. To modify the name value of the global namespace in a function, you need to use the global keyword x = 1
def foo2():
    global x
    x = 2
print(x)  #2

x = 1
def foo2():
    x = 2
    def foo3():
        nonlocal x
        x = 3
        print(x)  x=3 




Recommended Today

RCAST 35: add type to currency

– font ALT: Simsun; MSO font charset: 134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:”Cambria Math”; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; Variable; Ose-1: 216301111; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style […]