[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>1. 上下文管理器
一个类只要实现了 __enter__() 和 __exit__() 这个两个方法,通过该类创建的对象我们就称之为上下文管理器。
上下文管理器可以使用 with 语句,with语句之所以这么强大,背后是由上下文管理器做支撑的,也就是说刚才使用 open
函数创建的文件对象就是就是一个上下文管理器对象。
自定义上下文管理器类,模拟文件操作:
定义一个File类,实现 __enter__() 和 __exit__() 方法,然后使用 with 语句来完成操作文件, 示例代码:
# 自定义上下文管理器类 class File(object): def __init__(self, file_name, file_mode): self
.file_name = file_name self.file_mode = file_mode def __enter__(self): #
上文方法,负责返回操作对象资源,比如:文件对象,数据库连接对象 self.file = open(self.file_name, self.file_mode)
return self.file def __exit__(self, exc_type, exc_val, exc_tb): #
下文方法,负责释放对象资源,比如:关闭文件,关闭数据库连接对象 self.file.close() print('over') # with语句
结合上下文管理器对象使用 with File('1.txt', 'r') as f: # content = f.read() # print(content)
f.write('qqq') # 报错,但是仍然执行了关闭连接操作
代码说明:
* __enter__ 表示上文方法,需要返回一个操作文件对象
* __exit__ 表示下文方法,with语句执行完成会自动执行,即使出现异常也会执行该方法
<>2. 上下文管理器装饰器方式实现
假如想要让一个函数成为上下文管理器,Python 还提供了一个 @contextmanager 的装饰器,更进一步简化了上下文管理器的实现方式。通过
yield 将函数分割成两部分,yield 上面的语句在__enter__ 方法中执行,yield 下面的语句在 __exit__ 方法中执行,紧跟在
yield 后面的参数是函数的返回值。
from contextlib import contextmanager # 加上装饰器,那么下面函数创建的对象就是一个上下文管理器
@contextmanagerdef my_open(file_name, file_mode): global file try: file = open(
file_name, file_mode) # yield关键字之前的代码可以认为是上文方法,负责返回操作对象资源 yield file except
Exceptionas e: print(e) finally: # yield关键字后面的代码可以认为是下文方法,负责释放操作对象的资源 file.close
() print('over') # 普通函数不能结合with语句使用 with my_open('1.txt', 'r') as file: #
content = file.read() # print(content) file.write('1')