Does Pythonic have an automatic way for __exit__all class members?
class C:
def __init__(self):
self.a = open('foo')
self.b = open('bar')
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.a.__exit__(self, exc_type, exc_value, traceback)
self.b.__exit__(self, exc_type, exc_value, traceback)
Can I do this without manually calling __exit__on aand b? I even call __exit__right?
Suppose the resources that I have are not file, as in the example, and there is no method like closeor destroy. Is it possible, for example, to apply a method like this on top of __enter__and __exit__?
source
share