【第14天】Python第一阶段学习总结
2021/10/08
一. 装饰器作用
1. 作用
装饰器的作用:给已经写好的函数新增功能
2. 给函数新增功能
-
方案一:接修改原函数,把新增功能添加进去
存在的问题:如果多个函数都需要新增相同的功能,相同功能的代码需要写多遍
-
方案二:实参高阶函数
-
方案三:使用装饰器
二. 装饰器语法
1. 固定结构
-
装饰器 = 实参高阶函数 + 返回值高阶函数 + 糖语法
def 装饰器名称(旧函数): def 新函数(*args, **kwargs): result = 旧函数(*args, **kwargs) 实现新增的功能 return result return 新函数
# 练习1:写一个统计函数执行时间的装饰器 def total_time(fn): def new_fn(*args, **kwargs): start = time.time() time.sleep(randint(1, 2)) result = fn(*args, **kwargs) end = time.time() print(f'总时间:{end - start}s') return result return new_fn @total_time def func1(x): print(x * 2) func1(200)
# 练习2:写一个装饰器在原函数的开头打印start def start_func(fn): def new_fn(*args, **kwargs): print('start') result = fn(*args, **kwargs) return result return new_fn @start_func def func1(x): print(x * 2) func1(500)
# 练习3:将返回值是数字的函数的返回值变成原来的10000倍 def times_10000(fn): def new_fn(*args, **kwargs): result = fn(*args, **kwargs) if type(result) in (int, float): return result * 10000 return result return new_fn @times_10000 def func1(x): return x print(func1('123')) print(func1(100))
三. 模块的导入
1. 模块
-
一个py文件就是一个模块
-
可以在一个模块中使用另外一个模块中的内容,前提:
-
被另外一个模块使用的模块的名字必须符合变量名的要求
-
被使用之前需要导入
-
2. 导入模块(重要)
- import 模块名:直接导入指定模块,导入后能通过“模块名.”的方式使用模块中所有的全局变量
- from 模块名 import 变量名1,变量名2,变量名3,… :通过模块导入指定变量,导入后直接使用指定的变量
- from 模块名 import * :通过模块导入模块中所有全局变量,导入后直接使用指定的变量
- 重命名
import 模块名 as 新模块名 :使用模块的时候用新模块来使用
from 模块名 import 变量名1 as 新变量名1,变量名2,变量名3,…
# ====================导入方式1================
import test1
print(test1.x)
print(test1.a)
test1.func1()
import random
random.randint(10, 20)
# ====================导入方式2================
from test1 import a, func1
print(a)
func1()
print(x) # NameError: name 'x' is not defined
# ====================导入方式3================
from test1 import *
print(a)
print(x)
func1()
# ====================导入方式4================
import test1 as ts1
test1 = 'hello'
print(test1, ts1.a)
ts1.func1()
from test1 import x as t_x, a
x = '小明'
print(x, t_x)
print(a)
四. 导入模块的原理
1. 原理
- 当代码执行到导入模块的时候,系统会自动进入该模块讲模块中所有的代码都执行
2. if语句
- 这个if语句的特点:在if语句中的代码当前模块被别的模块导入的时候不会执行;如果直接运行当前模块又会执行
if __name__ == '__main__':
pass
五. 包的导入
1. 导入包中的模块(重要)
-
import 包名:要修改_ _ init _ _.py文件才有意义,否则无用
-
功能一:导入包中所有的模块,让在外部直接导入包的时候可以通过包去使用包中所有的模块
from file_manager import csvFile, jsonFile, textFile
-
功能二:建立常用方法和数据的快捷键
write_dict = jsonFile.write_dict() from file_manager.jsonFile import write_dict
-
功能三:封装通用函数
def open_file(): print('打开文件')
-
-
import 包名.模块名
-
from 包名 import 模块名1,模块名2,…
-
from 包名.模块名 import 变量1,变量2,…
# =================导入方式1======================= import file_manager
# =================导入方式2======================= # a. 不重命名 import file_manager.csvFile print(file_manager.csvFile.aa) file_manager.csvFile.read_file() # b. 重命名 import file_manager.csvFile as csvFile print(csvFile.aa) csvFile.read_file()
# =================导入方式3======================= from file_manager import csvFile, jsonFile, textFile print(csvFile.aa) jsonFile.write_dict()
# =================导入方式4======================= from file_manager.csvFile import read_file read_file()
2. 导入包的原理
导入包中的模块的内容的时候,系统会先执行包中_ _ init _ _.py文件中的内容,再执行模块中的内容
# 功能一
import file_manager
file_manager.csvFile.read_file()
file_manager.textFile.del_file()
# 功能二
from file_manager import write_dict
write_dict()
import file_manager
file_manager.write_dict()
file_manager.open_file()
from file_manager import open_file
open_file()
六. 数学模块
1. math和cmath
math :普通数字相关的数学函数
cmath:复数相关的数据函数
普通数字:100、-23.8、0.23
复数(complex):a+bj (a-实部,b-虚部,j是虚数单位;j**2 == -1)
import math, cmath
x = 10 + 2j
y = 20 + 1j # 1必须写
z = 20 - 3j
print(type(x))
print(x + z) # 30 - 1j
print(x * z) # 206+10j
-
ceil(浮点数) - 将浮点数转换成整数(向大取整)
print(math.ceil(1.9)) print(math.ceil(1.1))
-
floor(浮点数) - 将浮点数转换成整数(向小取整)
print(math.floor(1.9)) print(math.floor(1.1))
-
round() - 将浮点数转换成整数(四舍五入)
print(round(1.9)) # 2 print(round(1.1)) # 1
2. random
-
randrange(M, N, step) - 随机整数
import random print(random.randrange(10, 20, 2))
-
randint(M, N) - 随机整数
print(random.randint(10, 20))
-
random() - 产生0~1的随机小数
print(random.random()) # 0 ~ 1 print(random.random() * 10) # 0 ~ 10 print(random.random() * 9 + 1) # 1 ~ 10
-
choice(序列) - 从序列中随机获取一个元素
-
choices(序列, k=数量) - 从序列中随机获取指定数量的元素(有放回抽取)
nums = [1, 2, 3, 4, 5] print(random.choices(nums, k=3))
-
sample(序列, k=数量) - 从序列中随机获取指定数量的元素(无放回抽取)
print(random.sample(nums, k=3))
-
shuffle(列表) - 将序列中的元素的顺序随机打乱
random.shuffle(nums) print(nums)
七. 时间模块
-
获取当前时间
时间戳:以当前时间到1970年1月1日0分0秒(格林威治时间)的时间差来记录一个时间(时间差的单位是秒)
import time import datetime #'2021年10月8日 16:56:00' t1 = time.time() print(t1) # 1633684171.0057445
-
获取本地时间
localtime() - 获取当前的本地时间
t2 = time.localtime() print(t2, t2.tm_year)
localtime(时间戳) - 回去指定时间戳对应的本地时间
t3 = time.localtime(0) print(t3) t4 = time.localtime(1633684171.0057445) print(t4)
-
sleep(时间) - 让程序暂停运行;时间单位:秒
time.sleep(2) print('===============')
-
将结构体时间转换成时间戳
mktime(时间结构)
t5 = time.mktime(t2) print(t5)