Python学习笔记(二十一)- 模块 —— 总揽(Modules -The Big Pictuture)

1.模块源代码文件如何成为模块对象?
答:当导入该模块时,该模块的源代码文件自动成为模块对象。 从技术上讲,模块的源代码在导入期间运行,一次执行一个语句,并且在该过程中把所有名称都分配成为模块对象的属性。

2.为什么可能需要设置 PYTHONPATH 环境变量?
答:您只需要将 PYTHONPATH 设置为从您正在工作的目录以外的目录(即交互式工作时的当前目录或包含顶级文件的目录)中导入。 事实上,这将是普通程序的常见情况。

3.命名模块导入搜索路径的五个主要组成。
答:模块导入搜索路径的五个主要组成是顶层脚本的主目录(包含它的目录),PYTHONPATH 环境变量中列出的所有目录,标准库目录,位于标准目录(library directories)中的.pth文件中列出的所有目录和第三方扩展安装的 site-packages 根目录。 其中,程序员可以自定 PYTHONPATH 和 .pth 文件。

4.命名Python可能为回应导入操作而加载的四种文件类型。
答:Python可能会加载源代码(.py)文件,字节代码(.pyc或.pyo)文件,C扩展模块(例如Linux上的.so文件或Windows上的.dll或.pyd文件),或者 包导入的相同名字的目录。 导入还可能加载更多奇特的东西,例如ZIP文件组件,Jython 版本的 Python 下的 Java 类,IronPython 下的 .NET 组件,以及没有文件存在的静态链接的C扩展。 实际上,使用 improt hooks,import 可以加载任意项。

5.什么是命名空间,模块的命名空间包含什么?
答:命名空间是一个独立的变量包,称为命名空间对象的属性。 模块的命名空间包含模块文件顶层代码分配的所有名称(即,不嵌套在def或class语句中)。 从技术上讲,模块的全局范围会变换为模块对象的属性名称空间。 模块的命名空间也可以通过导入它的其他文件的赋值来改变,尽管这通常不赞成(有关跨文件更改的缺点的更多信息,请参阅第17章)。

一些其它的东西:

1.

模块通常对应于Python程序文件,也对应于用外部语言编写的扩展。

2.

import 让导入者(importer)整体导入模块

from 让导入者从模块中找到特定的名字

im.reload 

3.

顶层文件(top-level file)被称为脚本(script) 程序的启动口

模块文件(module files)被脚本调用

模块文件不做事情,它们是给其它文件导入使用的模块

4.

import b

大致意思是:加载文件(b.py),除非它已经被加载,通过名字b,给我访问它的所有属性的权限。

b.spam

大致的意思是:获取对象内存在的名为spam的值

5.

import语句做的3件事(3个步骤):

第一步,找到module文件

第二步,把它编译为字节码(byte code)【在需要的情况下】

第三步,运行模块文件以建立它所定义的对象(模块对象)
 

注:转载《Learning Python 5th Edition》[奥莱理]

1. How does a module source code file become a module object?
2. Why might you have to set your PYTHONPATH environment variable?
3. Name the five major components of the module import search path.
4. Name four file types that Python might load in response to an import operation.
5. What is a namespace, and what does a module’s namespace contain?

1. A module’s source code file automatically becomes a module object when that module is imported. Technically, the module’s source code is run during the import, one statement at a time, and all the names assigned in the process become attributes of the module object.
2. You only need to set PYTHONPATH to import from directories other than the one in which you are working (i.e., the current directory when working interactively, or the directory containing your top-level file). In practice, this will be a common case for nontrivial programs.
3. The five major components of the module import search path are the top-level script’s home directory (the directory containing it), all directories listed in the PYTHONPATH environment variable, the standard library directories, all directories listed in .pth path files located in standard places, and the site packages root directory for third-party extension installs. Of these, programmers can customize PYTHONPATH and .pth files.
4. Python might load a source code (.py) file, a byte code (.pyc or .pyo) file, a C extension module (e.g., a .so file on Linux or a .dll or .pyd file on Windows), or a directory of the same name for package imports. Imports may also load more exotic things such as ZIP file components, Java classes under the Jython version of Python, .NET components under IronPython, and statically linked C extensions that have no files present at all. In fact, with import hooks, imports can load arbitrary items.
5. A namespace is a self-contained package of variables, which are known as the attributes of the namespace object. A module’s namespace contains all the names assigned by code at the top level of the module file (i.e., not nested in def or class statements). Technically, a module’s global scope morphs into the module object’s attributes namespace. A module’s namespace may also be altered by assignments from other files that import it, though this is generally frowned upon (see Chapter 17 for more on the downsides of cross-file changes).

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/88046213