zoukankan      html  css  js  c++  java
  • with补充知识点

    import threading,queue,time
    import contextlib
    
    @contextlib.contextmanager
    def fun(list_1,val):
        list_1.append(val)
        try:
            yield
        finally:
            list_1.remove(val)
    
    
    q = queue.Queue()
    
    q.put('alxe')
    li = []
    
    with fun(li,1):
        q.get()
    

      

    class A:
        def __enter__(self):
            print ('__enter__() called')
    
        def __exit__(self, e_t, e_v, t_b):
            print ('__exit__() called')
    
    with A() as a:
        print('got instance')
    

      

    from __future__ import with_statement
    from contextlib import contextmanager
    
    @contextmanager
    def context():
        print ('entering the zone')
        try:
            yield
        except Exception as e:
            print ('with an error %s'%e)
            raise e
        else:
          print ('with no error')
    
    with context():
        print ('----in context call------')
    

      

    文件打开关闭
    import contextlib
    
    @contextlib.contextmanager
    def myopen(file_path,mode):
        f = open(file_path,mode,encoding='utf-8')
        try:
            yield f
    
        finally:
            f.close()
    
    with myopen('D:Esemantic/sd.txt','r') as file_obj:
        print(file_obj.read())
    

      

  • 相关阅读:
    MSSQL数据库 事务隔离级别
    CSS(Cascading Style Shee)
    Winform MD5
    Winform 基础
    ASP.NET 设置DropDownList的当前选项
    如何彻底关闭退出vmware虚拟机
    Winform GDI+
    SQL优化
    登录
    Spring AOP的应用
  • 原文地址:https://www.cnblogs.com/cloniu/p/6287331.html
Copyright © 2011-2022 走看看