Nested namespace functions, as well as the scope

Nested namespace functions, as well as the scope

1, call the function of three forms

def func():
    print('from func')
func()#第一种函数名加括号


def max_2(x,y):
    if x > y:
        return x
    else:
        return y
res = max_2(10,3)
print(res)#表达式去调用


def max_2(x,y):
    if x > y:
        return x
    else:
        return y
res = max_2(10,max(2,3))
print(res)#函数作为参数的形式

2, the nested function

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

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

func2()
>>>from func1
from func2

3, function namespace

  • What is the name space?

    Name space is used to store the name space.

  • The role of namespaces

    If you want to access a variable value, you must first access the corresponding name space, get a binding relationship corresponding memory address

  • Category namespace

    1, built-in namespace:

    Is to start a python interpreter name can be used, such as print, len, max, input and so on until we have not been defined, can be directly used to use the name, these names are stored in the built-in namespace

    2, the global namespace:

    Storage of file-level name is global name space, is in turn loaded into memory during execution of the program from top to bottom, and placed we set up all the variables and function names, if, while, for, internally defined name after the execution are stored in the global namespace

    3, the local name space

    All names inside the function is defined, noted that when the function is called, will produce the name space, with the end of the function performed, namespace disappears

  • The life cycle:

    1, built-in namespace

    When python interpreter started to take effect, close the python interpreter when failure

    2, global name space

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

    3, the local name space

    When you call the function to take effect when the function becomes ineffective once the call is completed

  • Use of namespaces

    1, the local name can be global, the built-in name

    x = 1
    def func():
        print(x)#使用了全局名称x,以及使用了内置名称print
    
    func()
    >>>1

    2, may be used built in the global namespace, but not using a local namespace

    x = 1
    def func():
        a = 1
        print(x)#使用了全局名称x,以及使用了内置名称print
    
    func()
    print(a)#NameError: name 'a' is not defined
            #此时a已经消失了
    

    3, can not be used in built local and global name

    x = 1
    def func():
        x = 2
        print(x)#使用了局部名称x
        def func1():
            x = 3#我自己有就用我自己的,没有就去找上一级,如果上一级都没去找内置,如果内置都没有那就保存
            print(x)
        func1()
    
    func()
    >>>2
    >>>3
    
    x = 1
    def func():
        x = 2
        print(x)#使用了局部名称x
        def func1():
            print(x)#我自己没有就用上一级的x=2
        func1()
    
    func()
    >>>2
    >>>2
    
    x = 1
    def func():
        print(x)
        def func1():
            print(x)#我自己没有就用上一级的,上一级也没有就再找上一级
        func1()
    
    func()
    >>>1
    >>>1
    
    x = 1
    
    def index(arg = x):#默认参数,此时已经将x=1赋值给arg
        print(x)
        print(arg)
    
    x = 2
    index()
    >>>2
    >>>1

4, the scope of 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 can call the name stored in the local scope

The local name space

For immutable types, you can view a local variable in global scope. But it can not be modified directly,

If you want to modify, you need to add a global variable in the program, this variable will be global variables are valid in all local operations

x = 1
def func():
    global x#声明此时的x就是全局变量
    x =2
    print(x)


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

#在局部修改外部函数的变量

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

    func1()
    print(x)#通过nonlocal就可以修改离他当前最近的一个外层变量的值,此时不会打印x=1,会打印x=3

func()
>>>3

Come on, Yafeng gogogo

Guess you like

Origin www.cnblogs.com/yafeng666/p/11837177.html