Home  >  Article  >  Backend Development  >  Introduction to three ways of namespace in python (with examples)

Introduction to three ways of namespace in python (with examples)

不言
不言forward
2018-10-09 16:48:473138browse

This article brings you an introduction to the three methods of namespaces in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

There are three types of namespaces in python:

The built-in namespace is the space where various names are automatically loaded into the memory when starting the interpreter, such as print, input, etc. Names that can be used as long as they are defined

The global namespace is the space where all the variable names and function names we define from top to bottom are located. It is loaded into the memory during the execution of the program from top to bottom. .

Local namespace, such as the space opened when a function is executed, stores various names defined within the function. This local namespace disappears as the function execution ends.

You can use the names in the global and built-in namespaces in the local namespace

You can use the names in the built-in namespace in the global namespace, but you cannot use the names in the local namespace.

Global and local names cannot be used in built-in namespaces.

It can be understood vividly that the built-in namespace has the highest level and can be used without definition, followed by the global namespace and the lowest level is the local namespace.

When a low-level namespace uses a name, it will first search in its own namespace. If it exists, it will be used. If not, it will search in the upper-level namespace. If it exists, it will be used. If not, it will continue to the next level. Search in the upper-level space until the top-level built-in namespace. If it exists, use it. If not, an error will be reported.

An example is as follows:

num = 1 #这是全局命名空间内定义的num = 1
def func1(): #这个函数会创建一个局部命名空间1
    num = 2  #这是在局部命名空间1定义的num = 2
    def func2(): #这个函数会创建一个局部命名空间2
        num = 3 #这是在局部命名空间2 内 定义的num = 3
        print(num) #这是在局部命名空间2内 使用num 变量
    func2() # 调用函数func2时 会执行该命令创建局部命名空间2
func1()  #调用函数func1时会创建局部命名空间1,
#这几个命名空间的级别顺序是:内置>全局>局部1>局部2
#当print(num)指令执行时它会在局部2内查找是否有num,发现有则直接引用,如果没有则会到上一级(局部1)中查找,局部1没有则继续到上一级(全局)查找

Look at another example:

#max()函数是内置的函数,如果我们在全局命名空间中定义了函数名为max的函数时,则该函数就失效了,也就是说在自身空间内找到了max()后就不去内置命名空间查找了
def max(a,b,c):
    return 'max失效了?'
print(max(1,2,3))#
正常情况下应该输出3(max函数功能是返回最大值),但是因为我们把max这个名字占用了,所以当使用时程序就不去上一级空间(内置命名空间)去查找了

The two functions globals() and locals()
related to the namespace can be used globals() View all defined names in the global namespace
You can use locals() to view all defined names in the current namespace. If it is global, it will return the global, and if it is local, it will return the local
The returns of these two functions The value can be regarded as a dictionary, and the format is {key1:value1,key2:value2,...}

When function 1 is nested within function 2, for the immutable data type in function 1 The variable can be referenced in function 2, but cannot be modified

def func1():
    a = 1
    def func2():
        a = a + 1 
#此处会报一个严重错误,因为a同时出现在等号两边,Python会先执行等号右边的表达式,
#发现你在引用a,然后它会先在本空间查找a,结果找到了等号左边的a,其结果就是“在定义变量前引用变量”
        pass      
    func2()
func1()
#如果想要修改,可以使用nonlocal(python3中新增)
def func1():
    a = 1
    def func2():
        nonlocal a
        a = a + 1
    func2()
    print(a) 
func1()
#我们可以看出,执行func2()后再输出a,a已经变成2了
    2

nonlocal can only act on local variables. When used, it will automatically search whether the upper-level local namespace has the variable. If not, continue to search upwards. , until the top level "local namespace"

The above is the detailed content of Introduction to three ways of namespace in python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more