Python学习笔记(二十二)- 模块编程基础 (Module Coding Basics)

1.你如何制作一个模块(module)?
答:要创建模块,你只需编写包含Python语句的文本文件; 每个源代码文件都自动成为一个模块,并且没有声明一个的语法。 导入操作将模块文件加载到内存中的模块对象中。 您也可以通过使用外部语言(如C或Java)编写代码来创建模块,但此类扩展模块超出了本书的范围。

2. from语句与import语句有什么关系?
答:from语句导入整个模块,如import语句,但作为额外步骤,它还将一个或多个变量从导入的模块复制到from出现的范围。 这使您可以直接使用导入的名称(name),而不必通过模块(module.name)。

3.reload函数与import语句有何关系?
答:默认情况下,每个进程只能导入一次模块(不能重复导入)。 reload函数强制再次导入模块。 它主要用于在开发期间和动态自定义方案中获取模块源代码的新版本

4.何时必须使用import而不是from?
答:只有当您需要在两个不同的模块中访问相同的名称时,才必须使用import; 因为您必须指定封闭模块的名称,这两个名称将是唯一的。 as扩展也可以在此上下文中使用(from module import name1 as name2)。

5.列举from语句的三个潜在缺陷。
答:from语句可以模糊变量的含义(它定义来自哪个模块中),可能在reload调用时遇到问题(名称可能引用对象的先前版本),并且可能破坏名称空间(它可能会无提示地覆盖您正在使用的名称) 你的范围)。 from *形式在大多数方面都更糟糕 - 它可能会严重破坏名称空间并模糊变量的含义,因此最好谨慎使用它。

6.空载燕子(swallow)的空中飞行速度是什么?(What is the airspeed velocity of an unladen swallow?)
答:你什么意思? 非洲或欧洲的燕子?(感觉又是一个梗,懂的人说下,想了解一波)

一些其它的什么东西:

1.import :

(1)识别外部要加载的文件

(2)变成脚本中的变量,引用了加载后的模块对象

2.from:

从一个文件复制特定的名字到另一个范围/作用域(scope)

3.*:

得到模块中所有的名字

4.improt/from 默认加载模块,1次且1个进程

5.像def, import from 为隐式赋值

(1)import将整个模块对象赋给一个名字

(2)from把1个或者多个名字赋给另一个模块中相同的名字

6.from module import name1, name2 这个操作等同于:

import module

name = module.name1

name = module.name2

del  module

(这也同时印证了之前所说的隐式赋值的含义)

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

1. How do you make a module?
2. How is the from statement related to the import statement?
3. How is the reload function related to imports?
4. When must you use import instead of from?
5. Name three potential pitfalls of the from statement.
6. What...is the airspeed velocity of an unladen swallow?

1.To create a module, you simply write a text file containing Python statements; every source code file is automatically a module, and there is no syntax for declaring one. Import operations load module files into module objects in memory. You can also make a module by writing code in an external language like C or Java, but such extension modules are beyond the scope of this book.

2.The from statement imports an entire module, like the import statement, but as an extra step it also copies one or more variables from the imported module into the scope where the from appears. This enables you to use the imported names directly (name) instead of having to go through the module (module.name).

3.By default, a module is imported only once per process. The reload function forces a module to be imported again. It is mostly used to pick up new versions of a module’s source code during development, and in dynamic customization scenarios.

4.You must use import instead of from only when you need to access the same name in two different modules; because you’ll have to specify the names of the enclosing modules, the two names will be unique. The as extension can render from usable in this context as well.


5.The from statement can obscure the meaning of a variable (which module it is defined in), can have problems with the reload call (names may reference prior versions of objects), and can corrupt namespaces (it might silently overwrite names you are using in your scope). The from * form is worse in most regards—it can seriously corrupt namespaces and obscure the meaning of variables, so it is probably best used sparingly.

6.What do you mean? An African or European swallow?

猜你喜欢

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