python的文件锁
Library Example - re

with 上下文管理器

erhuabushuo posted @ 2012年8月31日 01:04 in Python , 1010 阅读
#!/usr/bin/env python3

class ListTransaction:
    
    def __init__(self, theList):
        self.theList = theList

    def __enter__(self):
        self.workingcopy = list(self.theList)
        return self.workingcopy

    def __exit__(self, type, value, tb):
        if type is None:
            self.theList[:] = self.workingcopy
        return False

 

#!/usr/bin/env python3

from contextlib import contextmanager

@contextmanager
def ListTransaction(thelist):
    workingcopy = list(thelist)
    yield workingcopy
    # only do not raise error 
    thelist[:] = workingcopy

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter