day10 function object, nested namespace scoping

Function object

  • Function name can be cited:

    def index():
        print('from index')
    a = index  #函数名赋值给a。
    a()     #执行a
  • Function name can be passed as parameters

    def foo(x, y, z):
        print(x, y)
        z()
    
    def bar():
        print('from bar')
    
    foo(1, 2, bar)  # bar = z, z()=bar(),打印:from bar
  • Function name can be used as the return value

    *** parameter passing when there is no special requirements, must not parentheses, brackets executed on the spot.

    def index():            #第一步:定义函数没有执行
        print('from index')
    def func(a):            #第二步:定义函数
        return a            #第五步:输出from index
    a = func(index)         #第三步执行a,index = a
    a()                     #第四步执行a,相当于index().
  • Function name can be used as container type elements

    def func():
        print('from func')        #函数定义,没有执行
    
    l1 = [1, 2, 3, func, func()]  #func()直接执行,打印:from func
    f = l1[3]                     #func的出的时他的地址
    print(f)
    
    输出结果:
    from func
    <function func at 0x000001F8D7B63948>
    

Nested functions

  • Function nested definitions: defines a function in the interior

  • Nested function calls: calling a function inside the function

    def func1(x,y):
        if x > y:
            return x
        else:
            return y
    
    print(func1(1,2))
    
    def func2(x, y, z, a):
        result = func1(x, y)
        result = func1(result, z)
        result = func1(result, a)
        return result
    
    print(func2(1, 200000, 3, 1000))

Namespaces

  • What is the name space?

    Store name space

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

  • Namespace Category:

    1, built-in namespace:

    python advance to complete your definition of the name, is present in the built-in namespace

    2, global name space

    Stored in a file-level name is global name space

    if while for internally defined name after the execution are stored in the global name space

    3, the local name space

    The names of all internal functions are defined in the current function of storage and built-in namespace

    The life cycle:

    1, built-in namespace

    When python interpreter started to take effect, shut down the interpreter time to failure

    2, global name space

    When you start to take effect this current py file, after the end of the current page code execution failed

    3, the local name space

    When you call the entry into force of the current function, the function body end of the last line of code is executed on the failure to find the order of namespace:

Find the order

  • Namespace search order:

  • Local: Local> Global> Built

  • Global: Global> Built-in Built-in could not find on the error #

    Internal function uses the name specified in the definition phase has already died, and your call sites

    x = 111
    def func1():               #第一步
        x = 222
        def func2():           #第三步
            x = 333
            def func3():       #第五步
                x = 444
                def func4():   #第七步
                    x = 555    #第八步
                    print(x)
                    print('from func4')
                func4()        #第八步
            func3()            #第六步
        func2()                #第四步
    func1()                    #第二步
    x = 111    #是全局变量
    def index():
        def wrapper():
            print(x)
    
        return wrapper  #wrapper函数是在局部名称空间,
                         # return结束函数就没有内容了
    index()
    f = index()
    f()

Scope

  • Scope of the classification:

    1, the global scope

    Global can call the names present in the global scope

    Built-in namespace + global name space

    2, local scope

    Local storage can call on the name of the local scope

    The local name space

  • global: declare global variables
  • nonlocal: local namespace declarations local variables, modify variables in the upper function of local
  • Only a variable-type value can be changed in the local external variables

    golbal 全局变量使用
    x = 1
    def index():
        global x   #声明全局变量,使x = 2与x = 1在同一个级别,
                    #1先赋值给x,2又赋值给x。
        x = 2
    index()
    
    局部变量的修改无法影响上层,上上层
    
    def index():
        x = 1
    
        def index2():
            # nonlocal x
            x = 2
    
        index2()
        print(x)

Guess you like

Origin www.cnblogs.com/lishuangjian/p/11837737.html