python function scope

function scope in python

In python, a function is a scope

name = 'xiaoyafei'

def change_name():
    name = '肖亚飞'
    print('在change_name里的name:',name)

change_name()  # 调用函数
print("在外面的name:",name)

The running result is as follows:

在change_name里的name: 肖亚飞
在外面的name: xiaoyafei

Let's try again how to find in nested functions?

age = 15
def func():
    print('第一层age:',age)  # 第一层age: 15

    def func2():
        age = 73
        print("func2中的age:",age)  # func2中的age: 73

        def func3():
            age = 84
            print("func3中的age:",age)  # func3中的age: 84

        func3()  # 调用func3函数

    func2()  # 调用func2函数

func()

In the above nested functions, it can be well explained that a function is a scope, so let's change the code a little to see the situation?

age = 15
def func():
    print('第一层age:',age)  # 第一层age: 15

    def func2():
        print("func2中的age:",age)  # func2中的age: 15  # 看到没有,如果当前作用域里没有age变量,那么它就会往上找

        def func3():
            age = 84
            print("func3中的age:",age)  # func3中的age: 84

        func3()  # 调用func3函数

    func2()  # 调用func2函数

func()

Well, someone said at this time, a lot of bullshit is about local variables and global variables, so I want to ask: In the above nested function, there is no age variable in func2, so how does it find global variables? age = 15?

At this point we need to look at the search order of the scope:

Variable scope LEGB

  • L: locals namespace inside a function, including local variables and arguments
  • E: enclosing the namespace of the outer nested function, that is, the adjacent upper layer, for example: if there is no age variable in func2, it will go to func to find this
  • G:globals global variable
  • B:builtins Namespace for built-in modules

Ahem, let's first understand what a namespace is?

Name space , also known as name space , as the name implies, is the place to store the name, what name is stored? For example, x = 1, 1 is stored in the memory, then where is the variable name x stored? Name space is to store the name x and 1 where the bond is

>>> x = 1
>>> id(1)
1576430608

There are three types of namespaces:

  • locals: is the namespace within the function, including local variables and formal parameters
  • globals: global variables, the namespace of the module where the function is defined
  • builtins: namespace of built-in modules

The different scopes of different variables are determined by the namespace in which the variable is located.

scope is scope

  • Global scope: globally alive, globally valid
  • Partial scope: temporary stock, partial effective

Let's take an example

level = 'L0'
n = 22


def func():
    level = 'L1'
    n = 33
    print(locals())  # {'n': 33, 'level': 'L1'}

    def outer():
        n = 44
        level = 'L2'
        print(locals(),n)  # {'level': 'L2', 'n': 44} 44

        def inner():
            level = 'L3'
            print(locals(),n) # {'level': 'L3', 'n': 44} 44
        inner()
    outer()


func()

Search by the rules of L --> E --> G -->B, that is: if it is not found locally, it will go to the local outside the local (such as closure), and if it is not found, it will go to the global search. Then go to the built-in function to find.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325481040&siteId=291194637