python basis - function scope

function

Function object

  • Functions are first-class objects
  • Function name can be referenced
  • Function name can be used as parameters to use
  • Function name can be used as the return value use
  • Function name can be used as container type elements

Nested functions

  • Nested call : calling a function inside the function

  • Nested definitions : a function is defined inside the function

Namespaces

Storage variable name space, divided into built-in namespace , global namespace , local name space

  • Built-in namespace : python, the name defined in advance, there is built-in namespace. For example: keywords, built-in method

  • Global name space : stored in a file-level name is global name space

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

    # if 的示例:
    var = 4
    if var > 5:
        var = "var 大于5"
    else:
        var = "var 小于等于5"
    print(var)        # 打印结果:var 小于等于5
    
    # while 的示例:
    num = 0
    while num <= 3:
        num += 1
    print(num)        # 打印结果:4
    
    # for 的示例
    for i in range(2):
        pass
    print(i)      # 打印结果:1
  • The local name space : internal function definition all the names are stored in the current built-in namespace function

If you want to access a variable value, you must first access the corresponding name space, access to the relationship between the name and the names and values ​​of the memory address of the variable.

The life cycle

  • Built-in namespace : Python interpreter take effect when you start, fail closed
  • Global name space : the current python file execution into force, after the failure of execution
  • The local name space : the function becomes effective when called, the function body codes lapse at the end of the

Find the order

  • The local name : Local global built-in functions

  • Global Name : Global built

    Built-in error will not be found again

  • Internal function name used in the definition phase has been specified dead, with nothing to call the location

Scope

  • The global scope : global can call names exist in the global scope. Built-in namespace and global name space belong here
  • The local scope : Local can call the name exists in the local scope. The local name space belong here

  • , Ltd. Free Join : The local namespace variable declared as global variables

    Note: , Ltd. Free Join declare inside a function , if statement outside the function, the function is still not operating

    # 这种情况不能修改x1的值
    x1 = 1
    def func1():
        x1 = 2
    func1()
    print(x1)  # 打印结果:1
    
    # 使用global 后,可以修改x2的值
    x2 = 1
    def func2():
        global x2 # 先声明成全局变量
        x2 = 2
    func2()
    print(x2) # 打印结果:2
    
    # global 若在函数外声明,无法操作变量
    global x3
    x3 = 1
    def func2():
        x3 = 2
    func3()
    print(x3) # 打印结果:1
  • nonlocal : In the local namespace declarations local variables .

    After nonlocal variable declaration will start from the outer layers of a function of the current function to find the variable. If all the way to the outermost function can not be found, an exception is thrown.

    def f3():
        x = 4
        def f1():
            y = 2
            def f2():
                nonlocal x
                x = 3
            f2()  # 调用f2(),修改f1作用域中名字x的值
            print(x)  # 在f1作用域查看x。打印结果:3
        f1()
        print(x)  # 打印结果:3
    f3()
    def f3():
        x = 4
        def f1():
            x = 2
            def f2():
                nonlocal x
                x = 3
            f2()  # 调用f2(),修改f1作用域中名字x的值
            print(x)  # 在f1作用域查看x。打印结果:3
        f1()
        print(x)  # 打印结果:4
    f3()

Guess you like

Origin www.cnblogs.com/xiaodan1040/p/11837506.html