`
friendsys
  • 浏览: 339092 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Learning.Python.3rd.Edition 笔记 21章节

阅读更多
Chapter 18
Modules: The Big Picture

Modules Python中最高级别的程序组织单位,包装程序代码和数据用于重用,以文件为单位.
常见的相关关键字有
import
from
reload

模块import时,搜索的目录顺序
1:工程目录
2:配置的 PythonPath目录
3:标准类库目录
4:.pth文件中的目录

可以通过sys模块下的 path属性,来检查本机python搜索的路径,从左到右的顺序

凑数问题省略


Chapter 19
Module Coding Basics
模块编码基础

import 会将一个模块导入,访问时,需要使用模块名.方法()或属性, 进行操作

from module import 方法名, 将会把指定模块的方法导入到当前模块中,无须module名即可直接使用

前面说过很多次,使用from的时候,需要注意防止命名冲突,而且一样会加载整个外部module

from module1 import * 将会将模块所有内容导入


在加载模块时候,模块中的可执行代码将会被执行一次,如方法的调用和print等,如果在同一模块中再次import
该模块,那么将会不执行上述方法,也可以通过卸载的方法进行处理

和def一样,import和from可以用于动态加载

如果两个方法中,包含了同名的方法,而且同时使用from ..进行导入,那将会抛出异常,需使用import
可以使用c=com.orz.syss 的方式,将import进的module,缩短调用的代码

模块内置了一些属性可以使用,__name__,__file__
分别打印模块的完整引用名称与模块文件所在路径

运行时重新加载module,注意只能是Python编写的模块

使用内置函数 reload(object),传入import的 module对象
常用于在运行时,更新指定模块代码

凑数问题比较省略,本章介绍的关于原理的东西比较多,代码不多


Chapter 20
Module Packages
模块包

包中需要包含_ _init_ _.py文件,内容可为空
该文件代码,将会在第一次加载包下文件时执行

也可以在该文件中,设置from ..* 导入时,批量加载的module的具体类型
也可以声明变量,在包.属性变量名进行访问

其他方便的导入module方法
import dir1.dir2.mod as mod

可以使用子目录分别保存多个同名module,防止一级目录时,出现自动搜索出错的情况
注意添加对用的__init__.py文件

凑字数问题--比较短

Chapter Quiz
1. What is the purpose of an _ _init_ _.py file in a module package directory?
2. How can you avoid repeating the full package path every time you reference a
package’s content?
3. Which directories require _ _init_ _.py files?
4. When must you use import instead of from with packages?

Quiz Answers

1. The _ _init_ _.py file serves to declare and initialize a module package; Python
automatically runs its code the first time you import through a directory in a
process. Its assigned variables become the attributes of the module object created
in memory to correspond to that directory. It is also not optional—you
can’t import through a directory with package syntax, unless it contains this file.

2. Use the from statement with a package to copy names out of the package
directly, or use the as extension with the import statement to rename the path to
a shorter synonym. In both cases, the path is listed in only one place, in the from
or import statement.

3. Each directory listed in an import or from statement must contain an _ _init_ _.py
file. Other directories, including the directory containing the leftmost component
of a package path, do not need to include this file.

4. You must use import instead of from with packages only if you need to access the
same name defined in more than one path. With import, the path makes the references
unique, but from allows only one version of any given name.


Chapter 21
Advanced Module Topics
如果当前模块为主模块,可以使用__name__=='__main__' 进行判断

可以通过对sys.path修改,对搜索路径进行修改(临时性)
sys.path.append('c:\\lp3e\\examples')

无论是使用import还是from..import, 都可以使用as关键字进行别名
import longmodulename as name
from module import longname as name

还介绍了一些Python 2.7可能会提供的特性,这里不做记录

获取模块中对象的方法
M.name # Qualify object
M._ _dict_ _['name'] # Index namespace dictionary manually
sys.modules['M'].name # Index loaded-modules table manually
getattr(M, 'name') # Call built-in fetch function

也可以使用
for attr in module._ _dict_ _.keys( ):
对module中所有的对象进行循环,再调用上述的方法进行对象方法的调用


使用字符串导入模块,不能直接的使用字符串import
1: 方法一 exec方法,运行速度较慢
modname = "string"
exec "import " + modname # Run a string of code

2: 方法二 __import__内置函数, 速度较快,注意将返回一个module对象
modname = "string"
string = _ _import_ _(modname)

注意from模块导入时,并不能直接对原有模块域的值进行修改.
#nested1.py
X = 99
def printer( ): print X

#nested2.py
from nested1 import X, printer # Copy names out
X = 88 # Changes my "X" only!
printer( ) # nested1's X is still 99  //将不会进行修改

如果需要进行修改
import nested1 # Get module as a whole
nested1.X = 88 # OK: change nested1's X
nested1.printer( )

reload 在对From导入的module可能并不起作用,所以还是建议使用import和reload配合
关键问题在于,对导入该module 命名空间的function和对象不起作用

也可以使用 组合import和from,来达到reload的目的
import module
reload(module)
from module import function
function(1, 2, 3)

注意如果两个模块如果互相import,有可能导致异常

问题省略...这个章节介绍的东西,概念性的东西较多,并没有太多编码技术范畴的东西
















分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics