1. 异常
"""
异常介绍:
概述:
在Python中, 我们把程序运行时出现的 非正常情况, 统称为: 异常
常见的异常:
FileNotFoundError
除零错误
......
异常的默认处理方式:
程序会将异常的 类型, 产生原因, 异常出现的位置 打印到控制台上
并终止程序的执行
"""
src_f = open('1.txt', 'r')
print(10 // 0)
print('看看我执行了吗?')
1.1 异常-捕获
"""
捕获异常:
概述:
捕获异常这种方式处理完异常之后, 程序会 自动往下 继续执行
(基本)格式:
try:
可能出问题的代码
except:
出问题后的解决方案
执行流程:
1. 先执行 try中的内容, 看有无问题. 有问题会立即跳转到 except中执行
2. 如果try中内容无问题, 程序会跳过 except, 继续向下执行
"""
try:
print("try 1")
src_f = open('1.txt', 'r')
print("try 2")
except:
print('文件不存在, 请校验后重新操作!')
1.2 异常-捕获2
"""
格式:
try:
可能出问题的代码
except Exception as e:
出问题后的解决方案
格式解释:
Exception 所有异常的父类, 即: 它代表着所有的异常.
e 类似于我们以前写的变量名, 这里它是: 异常对象名.
细节:
还可以写成 except (异常1, 异常2) as e 这种写法是捕获多种异常.
"""
try:
print(name)
except Exception as e:
print(e)
print('看看我执行了吗?')
1.3 异常-传递性
"""
异常是具有传递性的, 函数内的异常 会传递给该函数的 调用者, 逐级传递, 直至这个异常被处理, 或者传递到main还不处理, 程序就会报错.
"""
def fun01():
print('----- fun01 start-----')
print(10 // 0)
print('----- fun01 end-----')
def fun02():
print('----- fun02 start-----')
fun01()
print('----- fun02 end-----')
def fun03():
print('----- fun03 start-----')
fun02()
print('----- fun03 end-----')
if __name__ == '__main__':
fun03()
2. 模块
"""
模块介绍:
概述:
模块指的是 Module, 在Python中, 1个.py文件 = 1个模块.
你可以把 模块理解为 工具包, 工具包中有很多的工具. 其实就是: 每个.py文件中都有很多的 函数, 这些函数都有不同的功能.
大白话:
学模块, 就是记忆一些 .py文件, 以及其中的一些 函数.
例如: 随机数相关 用 random模块, 日期相关用 time模块, 文件路径相关用 os模块...
模块的 导入方式:
方式1: import 模块名 后续通过 模块名.函数名() 的方式调用. 模块下所有函数 均可使用
方式2: import 模块名 as 别名 后续通过 别名.函数名() 的方式调用. 模块下所有函数 均可使用
方式3: from 模块名 import 函数名 后续通过 函数名() 的方式直接调用. 只能使用该模块下 导入的函数.
方式4: from 模块名 import 函数名 as 别名 后续通过 别名() 的方式直接调用. 只能使用该模块下 导入的函数.
方式5: from 模块名 import * 后续通过 函数名() 的方式直接调用. 模块下所有函数 均可使用
"""
import time
print('--- start ---')
time.sleep(2)
print(time.localtime())
print(time.time())
print('--- end ---')
import time as t
print('--- start ---')
t.sleep(2)
print(t.localtime())
print(t.time())
print('--- end ---')
from time import sleep, localtime
print('--- start ---')
sleep(2)
print(localtime())
print('--- end ---')
from time import sleep as sl, localtime as lt
print('--- start ---')
sl(2)
print(lt())
print('--- end ---')
from time import *
print('--- start ---')
sleep(2)
print(localtime())
print(time())
print('--- end ---')
2.1 模块-自定义
"""
案例: 演示如何调用 自定义模块.
细节:
1. 1个.py文件就可以看做是1个模块, 文件名 = 模块名, 所以: 文件名也要符合标识符的命名规范.
2. __name__属性, 当前模块中打印的结果是 __main__, 在调用者中打印的结果是: 调用的模块名
3. 如果导入的多个模块中, 有同名函数, 默认会使用 最后导入的 模块的函数.
4. __all__ 属性只针对于 from 模块名 import * 这种写法有效, 它只会导入 __all__记录的内容
"""
- 我是 module_1.py 文件中的内容
__all__ = ['fun01']
def fun01():
print('我是 module_1 模块的 函数')
print('----- fun01 函数 -----')
def fun02():
print('我是 module_1 模块的 函数')
print('----- fun02 函数 -----')
- 我是 test.py 文件中的内容
from module_1 import *
if __name__ == '__main__':
fun01()
fun02()
3. 包
"""
包 解释:
概述:
包 = 文件夹 = 一堆的.py文件(模块) + __init__.py 初始化文件.
背景:
当我们的模块(.py文件)越来越多的时候, 就要分包来管理它们了(模块).
导包方式:
方式1: import 包名.模块名
必须通过 包名.模块名.函数名() 的方式, 来调用 函数.
方式2: from 包名 import 模块名
必须通过 模块名.包名()的形式, 来调用函数.
"""
- 我是 my_module_1.py 文件中的内容
def fun01():
print('我是 my_module_1模块的 函数')
print('----- fun01 函数 -----')
- 我是 my_module_2.py 文件中的内容
def fun02():
print('我是 my_module_2 模块的 函数')
print('----- fun02 函数 -----')
- 我是 _ _init_ _.py 中的函数
__all__ = ['my_module1']
- 我是 test.py中的 函数
import my_package.my_module1
my_package.my_module1.fun01()
from my_package import my_module1
my_module1.fun01()
from my_package import *
my_module1.fun01()