zoukankan      html  css  js  c++  java
  • with上下文管理器

    问题思考:with打开文件为何会自动关闭?

    上下文管理器的概念:上下文管理器是一个python对象,为操作提供了额外的上下文信息,这种额外的信息,在使用with语句初始化上下文,以及完成with块中的所有代码时,采用可调用的形式。

    • object.__enter__(self)

      输入与此对象相关的运行时上下文,如果存在的话,with语句将绑定该方法的返回值到该语句的as子句中指定的目标。

    • object.__exit__(self,exc_type,exc_val,exc_tb)

      exc_type: #异常类型

      exc_val: #异常值

      exc_tb: #异常回溯追踪

    退出与此对象相关的运行时上下文,参数描述导致上下文退西湖的异常,如果该上下文退出时没有异常,三个参数都将为None,如果提供一个异常,并且该方法希望抑制该异常(即防止它被传播),它应该返回一个真值,否则,在退出此方法时,异常将被正常处理。

    class MyOpen():
    
        def __init__(self,file_name,open_method,encoding="utf-8"):
            self.file_name=file_name
            self.open_method=open_method
            self.encoding=encoding
    
        def __enter__(self):
            self.f=open(self.file_name,self.open_method,encoding=self.encoding)
            return self.f
    
    
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            print(exc_type)
            print(exc_val)
            print(exc_tb)
            self.f.close()
    
    
    with MyOpen("requirements.txt","r") as f:
        print(f)
    
    with MyOpen("requirements.txt", "r") as f:
        print(f+1)

    运行结果:

    <_io.TextIOWrapper name='requirements.txt' mode='r' encoding='utf-8'>
    None
    None
    None
    <class 'TypeError'>
    unsupported operand type(s) for +: '_io.TextIOWrapper' and 'int'
    <traceback object at 0x104d368c8>

     上下文管理器连接mysql数据库

    import pymysql
    
    #操作数据库的上下文管理器
    class DB(object):
    
        def __init__(self,data_conf):
            self.con=pymysql.connect(**data_conf)
            self.cursor=self.con.cursor()
        
        def __enter__(self):
            return self.cursor
        
        def __exit__(self, exc_type, exc_val, exc_tb):
            self.cursor.close()
            self.con.close()
        
    DATABASES_CONF=dict(
        host='localhost',
        user='root',
        password='mysql',
        database='test',
        port=3306,
        charset='uft-8'  
    )
    
    with DB(DATABASES_CONF) as cur:
        cur.execute("SELECT * from students")
        print(cur.fetchone())
  • 相关阅读:
    第三章,DNA序列的进化演变
    哈迪-温伯格定律和近亲杂交
    mysql
    Dajngo admin使用
    Django之Form组件
    python三大器
    python函数
    python文件操作
    python字典和集合
    算法基础
  • 原文地址:https://www.cnblogs.com/crystal1126/p/13563696.html
Copyright © 2011-2022 走看看