Answers for "how to break a with... as"

1

how to break a with... as

class fragile(object):
    class Break(Exception):
      """Break out of the with statement"""

    def __init__(self, value):
        self.value = value

    def __enter__(self):
        return self.value.__enter__()

    def __exit__(self, etype, value, traceback):
        error = self.value.__exit__(etype, value, traceback)
        if etype == self.Break:
            return True
        return error
        
with fragile(open(path)) as f:
    print 'before condition'
    if condition:
        raise fragile.Break
    print 'after condition'
Posted by: Guest on April-19-2021

Code answers related to "how to break a with... as"

Python Answers by Framework

Browse Popular Code Answers by Language