3 ms·
In a more complex application, an exception might be thrown before the last line that closes the file (and caught god knows where or not at all), so the files m
by nnq 14y ago
In a more complex application, an exception might be thrown before the last line that closes the file (and caught god knows where or not at all), so the files may not be closed... then again, if you actually try to catch the exceptions you get an extra level of indentation by using `with`.
More... "philosophy" here: http://stackoverflow.com/questions/11373502/zen-of-python-vs-with-statement-philosophical-pondering http://stackoverflow.com/questions/11373502/zen-of-python-vs...
- AceJohnny2 14y agoAh, so 'with' is a clearer syntax for exception handling, that's what I was missing. Thanks to you, masklinn and graue for clearing that up.
- nnq 14y agoGlad it helped you, but I wouldn't say "a clearer syntax for exception handling" really... more like a "way to ensure your code always works without having to actually think about exceptions". (Wrong as it may seem, I try to imagine I'm using "let" in a Lisp and then I instantly start to love the "with" syntax, although it's not a necessary part of Python :) )