how to break a with as

Solutions on MaxInterview for how to break a with as by the best coders in the world

showing results for - "how to break a with as"
Taina
10 Jun 2019
1class fragile(object):
2    class Break(Exception):
3      """Break out of the with statement"""
4
5    def __init__(self, value):
6        self.value = value
7
8    def __enter__(self):
9        return self.value.__enter__()
10
11    def __exit__(self, etype, value, traceback):
12        error = self.value.__exit__(etype, value, traceback)
13        if etype == self.Break:
14            return True
15        return error
16        
17with fragile(open(path)) as f:
18    print 'before condition'
19    if condition:
20        raise fragile.Break
21    print 'after condition'