Dier seventeen namespace and scope

Dier seventeen namespace and scope

Function inside the function only inside the function call, the function can not be called externally, in order to know why, we need to understand the scope and name space

First, the name space

Namespace (name spaces): In the memory management that chapter, we know that the variable is actually created in memory opened up a new space. But before storing the variable name has not been mentioned, in fact, there is room for a binding relationship between a block of memory to store the variable name and variable in memory, this space is called the namespace

1. built-in namespace

  • 1. built-in namespace: Store python interpreter that comes with the name, such as int、float、len、strip
  • 2. Life cycle: when the python interpreter starts to take effect, expire at the close python interpreter

2. The global name space

  • 1. Definitions: In addition to internal and local names, the rest are placed in the global namespace. Such as the followinga、f、l、i
  • 2. Life cycle: when the file is executed into effect, expire after file execution
a = 0

def f():
    pass

l = [2,'k']

if 2>1:
    if 0<1:
        i = 6

3. The local name space

  • 1. Definition: used to store the name of the function body generated during the function call. For example, the followingf2
  • 2. Life cycle: take effect during the function call when the file is executed, expire after the function execution
def f():
    def f2():
        print('ha')
    f2
    
f()

4. The loading sequence

  • Because .py file is opened by the python interpreter, so it must be built after the namespace in python interpreter has finished loading, began to open the file, this time will produce a global name space, but there is a certain function in the file when called, will begin to produce local name space, so the load order for the namespace: built-in namespace ----> global namespace ----> local name space

5. Find the order

  • As the name space is used to store a binding relationship between the variable name and value, so whenever you want to find the name, I have to find the name from one of the three spaces, the search order is to begin searching from the current location, If the current location is the local name space, the search order is: the local name space ----> global name space Agency ----> built-in namespace (that is, starting from the current location, only to find out)

    x = 0
    y = 1
    
    def f():
        x = 66
        y = 233
        print(x,y)
    
    f()   # 66 233
      ```python

    x = 0

def f():
print(x)

x = 6
f() #6
```

Second, the scope

  • Scope is the regional role of
  • == Just remember: global and local variables, it may be the same name, but the two are not the same at all ==

1. The global scope

  • Global Scope: Global effective, global survival, and includes built-in namespace global name space
x = 6

def f():
    print(x)
    
f()    # 6

2. Local Scope

  • The local scope: partially valid, temporary storage, only contains the local name space
def f1():
    def f2():
        def f3():
            print(x)
        x = 1
        f3()
    f2()
    
f1()      # 1

3. Notes

  • Scope relations function definition stage fixed die, regardless of the calling function
x = 1

def f():   # 函数定义阶段
    print(x)   # 由于f()函数的局部作用域没有x,所以会先去全局找,此时x=1
    
def f2():
    x = 6   # 这个x只能在f2()函数的局部作用域起作用,对f()函数没有丝毫影响
    f()   
    
f2()    # 1

4. Application of Function objects scope

def f():
    def f1():
        print('hah')
    return f1

a = f()   

def f2():
    a()
    
f2()      # hah

Third, additional knowledge point (not recommended)

1.global keyword

  • Role: Modify the variables global scope
x = 1

def f():
    x = 6
    def f2():
        x = 233
    f2()
    
f()
print(x)    # 在当前位置查找,所以是全局作用域的x=1
x = 1

def f():
    x = 6
    def f2():
        global x    # 可以将局部变量变为全局变量
        x = 233
    f2()
    
f()
print(x)    # 233

2.nonlocal keyword

  • Modify variables local scope
x = 1

def f():
    x = 6
    def f2():
        x = 233
    f2()
    print(x)
    
f()      # 6
x = 1

def f():
    x = 6
    def f2():
        nonlocal x     # 可以使内部函数变量在外部也能使用,但不能修改为全局变量
        x = 233
    f2()
    print(x)
    
f()      # 233

3. Notes

1. Local variable type you want to modify global, without any statement can be modified directly

2. In the immutable type partial want to modify global, it needs global statement, declared as global variables

lis = []

def f():
    lis.append(1)

# 函数调用前
print(lis)    # []

# 函数调用后
f()
print(lis)     # [1]

Guess you like

Origin www.cnblogs.com/itboy-newking/p/10953501.html