I saw some code in the Whoosh documentation:
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(u"ship")
results = searcher.search(query)
I read that the with statement executes __ enter__ and __ exit__ methods and they are really useful in the forms “with file_pointer:” or “with lock:”. But no literature is ever enlightening. And the various examples shows inconsistency when translating between the “with” form and the conventional form (yes its subjective).
Please explain
- what is the with statement
- and the “as” statement here
- and best practices to translate between both forms
- what kinds of classes lend them to with blocks
Epilogue
The article on http://effbot.org/zone/python-with-statement.htm has the best explanation. It all became clear when I scrolled to the bottom of the page and saw a familiar file operation done in with. https://stackoverflow.com/users/190597/unutbu ,wish you had answered instead of commented.
Example straight from PEP-0343: