Python命名空间详解
通俗的来说,Python中所谓的命名空间可以理解为一个容器。在这个容器中可以装许多标识符。不同容器中的同名的标识符是不会相互冲突的。理解python的命名空间需要掌握三条规则:
第一,赋值(包括显式赋值和隐式赋值)产生标识符,赋值的地点决定标识符所处的命名空间。
第二,函数定义(包括def和lambda)产生新的命名空间。
第三,python搜索一个标识符的顺序是"LEGB"。
所谓的"LEGB"是python中四层命名空间的英文名字首字母的缩写。
最里面的一层是L(local),表示在一个函数定义中,而且在这个函数里面没有再包含函数的定义。
第二层E(enclosing function),表示在一个函数定义中,但这个函数里面还包含有函数的定义,其实L层和E层只是相对的。
第三层G(global),是指一个模块的命名空间,也就是说在一个.py文件中定义的标识符,但不在一个函数中。
第四层B(builtin),是指python解释器启动时就已经具有的命名空间,之所以叫builtin是因为在python解释器启动时会自动载入__builtin__模块,这个模块中的list、str等内置函数的就处于B层的命名空间中。
这三条规则通过一个例子来看比较明白。如下面例子所示:
>>> g = int('0x3', 0) >>> def outFunc(): e = 2 g = 10 def inFunc(): l = 1 return g + e return inFunc() >>> outFunc() ===> 12
来详细看看这段代码中的标识符。
第1行,适用第一条规则“赋值产生标识符”,因此产生一个标识符g。“赋值的地点决定标识符所处的命名空间”,因为g是没有在一个函数定义中,因此g处于'G'层命名空间中。这一行中还有一个标识符,那就是int。那么int是在什么地方定义的呢?由于int是内置函数,是在__builtin__模块中定义的,所以int就处于'B'的层命名空间中。
第2行,适用第一条规则,由于def中包含一个隐性的赋值过程,这一行产生一个标识符outFunc,outFunc并不处于一个函数定义的内部,因此,outFunc处于'G'层命名空间中。此外,这一行还适用第二条规则,产生一个新的命名空间。
第3行,适用第一条规则,产生个标识符e,而且由于这是在一个函数定义内,并且内部还有函数定义,因此e处于'E'层命名空间中。
第4行要注意,适用第一条规则,产生一个标识符g,这个g与e一样外于'E'层命名空间中。这个g与第一行的g是不同的,因为所处的命名空间不一样。
第5行,适用第一条规则,产生一个处于'E'层命名空间的标识符inFunc。与第2行一样,这一行定义函数也产生一个新的命名空间。
第6行,适用第一条规则,产生一个标识符l,由于这个l处于一个函数内部,而且在这个函数内部没有其他函数的定义,因此l处于'L'层命名空间中。
第7行,适用第三条规则,python解释器首先看到标识符g,按照LEGB的顺序往上找,先找L层(也就是在inFunc内部),没有。再找E层,有,值为10。因此这里的g的值为10。寻找过程到为止,并不会再往上找到'G'层。寻找e的过程也一样,e的值为2。因此第9行的结果为12。
其实,所谓的“LEGB”是为了学术上便于表述而创造的。让一个编程的人说出哪个标识符处于哪个层没有什么意义,只要知道对于一个标识符,python是怎么寻找它的值的就可以了。其实找值的过程直观上也很容易理解。
通过上面的例子也可以看出,如果在不同的命名空间中定义了相同的标识符是没有关系的,并不会产生冲突。寻找一个标识符的值过程总是从当前层开始往上找的,首先找到的就为这个标识符的值。也由此可以这么说,'B'层标识符在所有模块(.py文件)中可用;'G'层标识符在当前模块内(.py文件)中可用;'E'和'L'层标识符在当前函数内可用。
再来看一个例子,来解释global语句的用法。代码如下所示:
>>> g = 'global' >>> s = 'in' >>> def out(): g = 'out' def inter(): global g print s,g inter() >>> out() ===> 'in global'
可以看到,虽然有两个层中的g,但使用了global语句后,就是指'G'层的标识符。也就是第7行中的g,就是指第1行产生的那个g,值为'global'。
最后说一句,其实只要在编程的时候注意一下,不要使用相同的标识符,基本上就可以避免任何与命名空间相关的问题。还有就是在一个函数中尽量不要使用上层命名空间中的标识符,如果一定要用,也最好使用参数传递的方式进行,这样有利于保持函数的独立性。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
