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())
    

      

  • 相关阅读:
    MATLAB01
    Diffie-Hellman 密钥交换
    古典密码
    正则表达式
    装饰器初析
    进制转换的栈实现
    Log4j(异常日志)
    2018/6/6
    2018.1.1T19B3-u4
    2018.1.1T19-B3-U3jiangyi
  • 原文地址:https://www.cnblogs.com/cloniu/p/6287331.html
Copyright © 2011-2022 走看看