Nesting, namespace, and scope of functions

Time:2020-1-21

Nesting, namespace, and scope of functions

1. Three forms of calling function

def func():
    print('from func')
Func() ාා񖓿ාthe first function name is bracketed


def max_2(x,y):
    if x > y:
        return x
    else:
        return y
res = max_2(10,3)
Print (RES) - expression to call


def max_2(x,y):
    if x > y:
        return x
    else:
        return y
res = max_2(10,max(2,3))
Print (RES) function as a parameter

2. Nesting of functions

def func1():
    print('from func1')

def func2():
    func1()
    print('from func2')

func2()
>>>from func1
from func2

Nesting, namespace, and scope of functions

3. Function namespace

  • What is a namespace?

    A namespace is a space for names.

  • The role of namespaces

    If you want to access a variable value, you must first access the corresponding namespace and get the binding relationship of the corresponding memory address

  • Classification of namespaces

    1. Built in namespace:

    The names that can be used as soon as the Python interpreter is started, such as print, len, Max, input, etc., which we have not defined before, can be used directly. These names are stored in the built-in namespace

    2. Global namespace:

    The name of the file level is the global namespace, which is loaded into the memory in sequence during the execution of the program from top to bottom. After the execution of all variable names and function names, if, while, for, the internally defined names are stored in the global namespace

    3. Local namespace

    This is all the names defined in the function. Note that this namespace will be generated when the function is called. As the function execution ends, the namespace will disappear

  • Life cycle:

    1. Built in namespace

    It takes effect when the Python interpreter is started and fails when the Python interpreter is closed

    2. Global namespace

    When you start the PY file, it will take effect. The current page code will expire after execution

    3. Local namespace

    It takes effect when you call a function. Once the function is called, it will fail

  • Scope of namespace usage

    1. Global name and built-in name can be used locally

    x = 1
    def func():
        Print (x) ා uses the global name x and the built-in Name Print
    
    func()
    >>>1

    2. Built in namespaces can be used globally, but not locally

    x = 1
    def func():
        a = 1
        Print (x) ා uses the global name x and the built-in Name Print
    
    func()
    print(a)#NameError: name 'a' is not defined
            #Now a is gone

    3. Local and global names are not allowed in built-in

    x = 1
    def func():
        x = 2
        Print (x) × local name x used
        def func1():
            X = 3 × if I have my own, I will use my own, if not, I will go to the next level, if not, I will save it
            print(x)
        func1()
    
    func()
    >>>2
    >>>3
    
    x = 1
    def func():
        x = 2
        Print (x) × local name x used
        def func1():
            Print (x) ා if I don't use the previous x = 2
        func1()
    
    func()
    >>>2
    >>>2
    
    x = 1
    def func():
        print(x)
        def func1():
            Print (x) ා if I don't use the upper level by myself, I will find the higher level if I don't use the upper level
        func1()
    
    func()
    >>>1
    >>>1
    x = 1
    
    Def index (Arg = x): default parameter, x = 1 has been assigned to arg
        print(x)
        print(arg)
    
    x = 2
    index()
    >>>2
    >>>1

    Nesting, namespace, and scope of functions

4. Scope classification

1. Global scope

Global callable names exist in the global scope

Built in namespace + global namespace

2. Local scope

Locally callable names are stored in the local scope

Local namespace

For immutable types, variables in the global scope can be viewed locally. But it can’t be modified directly,

If you want to modify it, you need to add a global variable to the program, then all operations of this variable in the local area will be valid for the global variable

x = 1
def func():
    Global x ා declare that x at this time is the global variable
    x =2
    print(x)


func()
print(x)
>>>2
>>>2

#Modify variables of external functions locally

def func():
    x = 1
    def func1():
        nonlocal x
        x = 3

    func1()
    Print (x) ා you can modify the value of the nearest outer variable through nonlocal. At this time, x = 1 will not be printed and x = 3 will be printed

func()
>>>3

Come on, Yafeng gogogo