I am having problems using with statement
( PEP 343 ) in python to automatically control resource cleanup after context. In particular, with statement
always assumes the .close()
resource cleanup method. I.E. in the next block of code, browser.close()
automatically called when execution ends out of context, however browser.close()
not the correct cleanup, since it closes the current window, not the entire browser. What was supposed to do was call browser.quit()
.
with contextlib.closing(webdriver.Firefox()) as browser: # do something with browser # at this point browser.close() has been called.
Unfortunately, contextlib.closing
does not provide a way to customize the name of the cleanup method that should be called, as you can see here :
def __exit__(self, *exc_info): self.thing.close()
However, I notice that the exec_info
argument exec_info
, but was not used in this particular method. Does anyone know why?
The bigger the question, the better, as the name, if possible, how to have self.thing to call an arbitrary cleaning method? If not, what is the best way to work? Should I revert to using try...finally
?
source share