The introduction of

To see the Zen of Python lore, run import this in the Python interpreter, which represents the specification for developing in Python, Let’s do more of those! Is the main character of this article.

Namespaces

A namespace is a place where names and objects are bound. It is a partition of stack areas. Define a variable. Name = ‘python’, the interpreter allocates memory space for the value ‘python’, and stores the binding relationship between the name name and the memory address of ‘python’ on the stack.

With namespaces, you can store the same name in the stack area, and there are up to three namespaces, each independent of the other, during program execution.

Built-in namespaces

They hold names built into the Python interpreter, such as reserved words (keywords) in Python. Built-in namespaces are created when the Python interpreter is started and destroyed when the Python interpreter is shut down.

Type print in the interactive interpreter environment and press Enter
>>> print
<built-in function print>
Copy the code

Global namespace

As long as it’s not a function defined or a built-in name, everything else is a global name. Global namespaces are created when a Python file is run and destroyed when the Python file is finished executing.

x = 1  # x is the global name
def func() :  # func is the global name
    print(x)  # print is the built-in name
Copy the code

Local namespace

Names defined within functions are stored in local namespaces. Local namespaces are created when a function is called and destroyed when the function is finished running.

def func() :  # func Global name
    x = 100  # x local name
    print(x)  # print built-in name
Copy the code

Namespaces and scopes

Namespaces are loaded in the following order: built-in namespace -> global namespace -> local namespace.

The namespace destruction sequence is: local namespace -> global namespace -> built-in namespace.

Three namespaces can be divided into two regions based on the scope of the names in the namespace.

Global scope – Global namespaces, names in built-in namespaces belong to the global scope. Global names are not destroyed during file execution and can be used anywhere.

Local scope – names in a local namespace belong to a local scope. Names in a local scope are created when a function is called, destroyed at the end of the call, and can only be used within a function.

Scope and name lookup priority

One rule to keep in mind when looking up names is that names are looked up in the function phase, not the call phase.

  • Start with the local scope

Look up the name in the local scope, start at the local scope, now look up the name in the local namespace, look up the name in the global namespace, look up the name in the global namespace, look up the name in the built-in namespace, and finally can’t find it and throw an exception.

  • Start with the global scope

When looking for a name in a global scope, the starting point is the global scope, the global namespace is searched, the built-in namespace is not found, and an exception is thrown.

  • “Nested relationships” of namespaces

The nesting here can be interpreted as the existence of the same variable name in different namespaces. Note: Be sure to follow the function definition stage and remember the order in which namespaces are looked up.

global & nonlocal

If you want to change the value of a name in a global namespace within a function and the value is an immutable data type, you need to use the global keyword. If the value is a mutable data type, you can modify the original value without using global.

x = 100
l = [1.2]
def func() :
    global x  X is the name of the global namespace
    x = 101
    l.append(3)
func()
print(x)  # 101
print(l)  # [1, 2, 3]
Copy the code

For functions with multiple layers of nesting, you can use the nonlocal keyword to declare the name of the outer layer nested function definition and change its value. Nonlocal looks for the declared name layer by layer from the outer function of the current function and throws an exception if the name is not found in the outermost function namespace.

def func() :
    x = 10
    def func1() :
        nonlocal x  
        x = 11
    func1()  Change the value of x in the func scope
    print(x)  Check the value of x in the func scope
func()  # 11
Copy the code