python3笔记 7 包、模块、函数与变量作用域
7-1 while循环与使用场景
while循环和for循环
counter = 1
while counter <= 10:
counter += 1
print(counter)
else:
print('Eof') #while结束后会执行else
ctrl + c 结束死循环
递归适合用while
7-2 for与for-else循环
for主要是用来遍历/循环 序列或者集合、字典
对应于其他语言中的for each
a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
for y in x:
print(y,end = ' ') #一横行打印
else:
print('fruit is gone')
利用break和continue跳过某些字符:
a = [1,2,3]
for x in a:
if x == 2:
continue
print(x)
利用break打断的for循环不会执行else里的语句,而continue会执行else
a = [1,2,3]
for x in a:
if x == 2:
break
print(x)
else:
print('EOF') #此句不执行
循环二维数组时跳出
a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
for y in x:
if y == 'orange':
break #只跳出了内层循环
print(y,end = ' ')
else:
print('fruit is gone')
7-3 for 与 range
利用range()打印0-9
for x in range(0,10):
print(x)
rang(首,跨度,步长)打印0,2,4,6,8
for x in range(0,10,2):
print(x,end = ' | ')
#0 | 2 | 4 | 6 | 8 |
递减的等差数列:
for x in range(10,0,-2):
print(x,end = ' | ')
#10 | 8 | 6 | 4 | 2 |
7-4 新篇章导言
高性能、封装性、可复用的代码考验逻辑能力和抽象能力
7-5 Python工程的组织结构:包、模块儿、类
最顶级的组织结构:包(文件夹)
第二个层级:模块(文件)
第三个层级:类
第四个层级:函数、变量(不属于组织结构,是类本身的特性)
7-6 Python包与模块的名字
区分不同包的同名模块:包名.模块名
形成的模块的路径叫做命名空间
一个包下面可以有子包,模块可以和包平级
普通文件夹想要变成包必须要有__init__.py文件
__init__.py本身也是一个模块,可以不写内容只是标注包
特殊地,__init__.py模块的名字就是包名
7-7 import导入模块
对于重复的定义需要从其他模块里引用。
利用 import 模块名 导入
#test1.c1
a = 1
#test1.c2
import c1
print(c1.a)
#1
import导入的总是模块,需要用模块名.变量名的方法引用
可以用as简化书写
#test1.c1
a = 1
#test1.c2
import c1 as m
print(m.a)
#1
优点是可以一眼看出属于哪个模块
7-8 from import 导入变量
#test1.c2
from c1 import a
print(a)
也可以from 包 import 模块
引用时用 模块.变量
引用大量变量时使用 * :
#test1.c1
a = 1
b = 2
c = 3
d = 4
#test1.c2
from c1 import *
print(a)
print(b)
print(c)
print(d)
控制*的行为:
#test1.c1
__all__ = ['a','c'] #用内置变量__all__来控制*的选择范围
a = 1
b = 2
c = 3
d = 4
#test1.c2
from c1 import *
print(a)
print(c)
print(d) #d没有被打印,报错
7-9 __init__.py 的用法
隐藏__pycache__文件夹
代码换行:在上一行末尾加上\或者加上括号利用括号的特性换行
当包被导入时,__init__.py会首先自动被执行
#test1.__init__.py
a = 'This is a __init__.py file'
print(a)
#c8.py
import test1
#This is a __init__.py file
__init__.py的应用场景:
#test1.__init__.py
__all__ = ['c1','c2'] #初始化*
#test1.__init__.py
import sys #批量导入库
import datetime
import io
#c8.py
import test1
print(test1.sys.path)
7-10 包与模块的几个常见错误
包和模块是不会被重复导入的,只会执行一次(入口文件的概念)
避免循环导入,不要形成闭环
导入模块的时候会执行模块里所有的代码
7-11 模块内置变量
#test1.c3
infos = dir() //打印当前模块内所有变量
print(infos)
#['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
#test1.c4.py
'''
This is a c4 doc
'''
print('name:' + __name__)
print('package:' + __package__)
#print('doc:' + __doc__) #doc指的是模块的注释
print('file:' + __file__)
#c9.py
import test1.c4
#name:test1.c4
#package:test1
#doc:
# This is a c4 doc
#file:c:\Users\Tai Park\Documents\python\test1\c4.py
7-12 入口文件和普通模块内置变量的区别
#c9.py
'''
This is a c9 doc
'''
import test1.c4
print('_________________')
print('name:' + __name__)
print('package:' + (__package__ or '当前模块不属于任何包'))
print('doc:' + __doc__)
print('file:' + __file__)
name:test1.c4
package:test1
doc:
This is a c4 doc
#name:test1.c4
#package:test1
#doc:
# This is a c4 doc
#file:c:\Users\Tai Park\Documents\python\test1\c4.py
#_________________
#name:__main__ #入口文件中__name__会被强制修改为__main__
#package:当前模块不属于任何包
#doc:
# This is a c9 doc
#
#file:c9.py #入口文件中路径不同 ,和执行python命令所在目录是有关系的
7-13 __name__的经典应用
dir()可以打印当前所有变量,若不传参数显示所有,传参显示特定的变量
import sys
infos = dir(sys)
print(infos)
make a script both importable and executable:
if __name__ == '__main__':
print('This is app')
else:
print('This is a module')
cmd中 python -m 命名空间.模块 可以将其当作模块来运行
作为普通模块必须要有包,可执行文件没有包
7-14 相对导入和绝对导入 一
有一个主入口文件。
可执行文件和顶级包在同一级。
绝对导入:从顶级包开始往下导入
相对导入:. 当前目录 ..上级目录 ...上上级目录
7-15 相对导入和绝对导入 二
相对导入不能超过顶级包。
入口不能用相对路径导入,相对路径根据__name__定位,而入口文件被强制改成了__main__所以不能使用。
若想在入口文件使用相对导入,用-m。
我的个人博客:点击打开链接