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

本文详细介绍了Python中的模块和包的概念及其使用方法,包括模块的导入方式、搜索路径、重新加载及包的基本操作等。
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,有可能导致异常

问题省略...这个章节介绍的东西,概念性的东西较多,并没有太多编码技术范畴的东西
基于51单片机,实现对直流电机的调速、测速以及正反转控制。项目包含完整的仿真文件、源程序、原理图和PCB设计文件,适合学习和实践51单片机在电机控制方面的应用。 功能特点 调速控制:通过按键调整PWM占空比,实现电机的速度调节。 测速功能:采用霍尔传感器非接触式测速,实时显示电机转速。 正反转控制:通过按键切换电机的正转和反转状态。 LCD显示:使用LCD1602液晶显示屏,显示当前的转速和PWM占空比。 硬件组成 主控制器:STC89C51/52单片机(与AT89S51/52、AT89C51/52通用)。 测速传感器:霍尔传感器,用于非接触式测速。 显示模块:LCD1602液晶显示屏,显示转速和占空比。 电机驱动:采用双H桥电路,控制电机的正反转和调速。 软件设计 编程语言:C语言。 开发环境:Keil uVision。 仿真工具:Proteus。 使用说明 液晶屏显示: 第一行显示电机转速(单位:转/分)。 第二行显示PWM占空比(0~100%)。 按键功能: 1键:加速键,短按占空比加1,长按连续加。 2键:减速键,短按占空比减1,长按连续减。 3键:反转切换键,按下后电机反转。 4键:正转切换键,按下后电机正转。 5键:开始暂停键,按一下开始,再按一下暂停。 注意事项 磁铁和霍尔元件的距离应保持在2mm左右,过近可能会在电机转动时碰到霍尔元件,过远则可能导致霍尔元件无法检测到磁铁。 资源文件 仿真文件:Proteus仿真文件,用于模拟电机控制系统的运行。 源程序:Keil uVision项目文件,包含完整的C语言源代码。 原理图:电路设计原理图,详细展示了各模块的连接方式。 PCB设计:PCB布局文件,可用于实际电路板的制作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值