2022-09-25 08:25阅读: 23评论: 0推荐: 0

上下文管理简单使用

构造

class Connect
	def __init__(self):
		pass
	def __enter__(self):
		#链接数据库
		return self
	def __exit__(self,exc_type,exc_val,exc_tb):
		#关闭数据库
		pass
	def fetchone(self):
		#链接数据库
		pass

使用

with Connect()as conn: #__enter__的返回值
	conn.fetchone()

本文作者:Sherwin

本文链接:https://www.cnblogs.com/sherwin1995/p/16727212.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Sherwin_szw  阅读(23)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.