python实现可以被with上下文管理的类或函数

开始之前先明确一下with机制
1.类包函数__enter__()__exit__()函数,即是可以被上下文管理的类
__enter__用来执行with时的方法,__exit__返回对象给with的as 对象
2.通过函数实现要用到contextmanager装饰器与yield关键字

一、通过类来实现支持whith上下文管理功能

class fp(object):
  def __init__(self, fileobj, mode):
    self.fileobj = open(fileobj, mode)

  def __enter__(self):
    return self.fileobj

  def __exit__(self, type, value, Traceback):
    self.fileobj.close()
    return True

with fp("abc1.txt", "w+") as fi:
  fi.write("测试上下文管理器")

二、通过函数yield关键字实现

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
from contextlib import contextmanager

@contextmanager
def fp(fileobj, mode):
      f = open(fileobj, mode)
      yield f
      f.close()


with fp("abc2.txt", "w+") as fi:
  fi.write("context测试上下文管理器")

用以上两种方法来实现相同的功能

posted @ 2022-10-07 20:29  I'm_江河湖海  阅读(11)  评论(0)    收藏  举报