Function basis - namespaces and scope

Function inside the function only inside the function call, you can not call outside the function?!
Reason: namespace problem !!!

Dian a namespace

Namespace (name spaces): When memory management that chapter, we said that to create a variable is actually opened up a new space in memory. But we've been avoiding storing variable names, in fact, there is room for a binding relationship between a variable name and variable memory storage in memory, and this space is called a namespace.

1.1 built-in namespace

Built-in namespace: Store Pyhton interpreter that comes with the name, such as int, float, len

Life cycle: when the interpreter started to take effect, fail when the interpreter is closed

1.2 global name space

Global name space: In addition to internal and local names, the rest are stored in the global name space, as in the following code
x、func、l、z
life cycle: take effect when the file is executed, the file expire after execution

x = 1

def func():
    pass

l = [1, 2]

if 3 > 2:
    if 4 > 3:
        z = 3

1.3 The local name space

The local name space: the name of the function body for storing generated during a function call, such as the following code f2

Life Cycle: take effect during the function call when the file is executed, expire after the function execution

def f1():
    def f2():
        print('from f2')
    f2()

f1() 

1.4 load order

Because .py file is opened by the Python interpreter, so it must be built after the name space 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, it will begin to produce local name space, the name space for the loading order: built-in - "global -" local.

Find the order of 1.5

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, it must be found in one of the three, look for the following order:
start looking from the current location, if the current location location for the local name space, look for the following order: local - "global -" built.

x = 1
y = 2
len = 100

def func():
    y = 3
    len = 1000
    print(f"y: {y}")
    print(f"len: {len}")
    # print(a)  # NameError: name 'a' is not defined

func()

y: 3 len: 1000

x = 1

def func():
    print(x)

x = 10
func()

Two Dian Scope

Domain refers to a region, i.e. the region scope of action.

2.1 global scope

Global Scope: Global effective, global survival, and includes built-in namespace global name space.

# 全局作用域
x = 1

def bar():
    print(x)

bar()

1

2.2 local scope

The local scope: local small, temporary storage, containing only the local name space.

# 局部作用域
def f1():
    def f2():
        def f3():
            print(x)
        x = 2
        f3()
    f2()

f1()

2

2.3 Notes

Note that: the scope of the relationship between the function definition stage fixed die, regardless of the function was called.

# 作用域注意点
x = 1

def f1():  # 定义阶段x=1
    print(x)

def f2():
    x = 2
    f1()

f2()

1

2.4 Application of scopes function object +

# 作用域应用
def f1():
    def inner():
        print('from inner')
    return inner

f = f1()  # 把局部定义的函数放在全局之中

def bar():
    f()

bar()

from inner


Wed and supplement knowledge

3.1 global keyword

Modify variables in global scope.

x = 1

def f1():
    x = 2

    def f2():
        #         global x  # 修改全局
        x = 3
    f2()

f1()
print(x)

1

x = 1

def f1():
    x = 2
    def f2():
        global x  # 修改全局
        x = 3
    f2()

f1()
print(x)

3

3.2 nonlocal keyword

Modify variables local scope.

x = 1

def f1():
    x = 2
    def f2():
        #         nonlocal x
        x = 3

    f2()
    print(x)

f1()

2

x = 1

def f1():
    x = 2
    def f2():
        nonlocal x
        x = 3

    f2()
    print(x)

f1()

3

3.3 Notes

 1. In the local want to modify the global variable type, does not require any statement can be modified directly.
 2. Local immutable type if you want to modify global, the need to use global statement, declared as global variables can be modified directly.

lis = []

def f1():
    lis.append(1)

print(f"调用函数前: {lis}")
f1()
print(f"调用函数后: {lis}")

调用函数前: [] 调用函数后: [1]

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374798.html