summaryrefslogtreecommitdiffhomepage
path: root/python.html.markdown
diff options
context:
space:
mode:
authorDenis Gladkikh <denis@gladkikh.email>2015-09-01 09:27:40 -0700
committerDenis Gladkikh <denis@gladkikh.email>2015-09-01 09:27:40 -0700
commita24e5ef7a115eb32e929a003fdfcdb3704927064 (patch)
tree82af2440601b8c5f2b9ac23a9cecdbc9879c20b3 /python.html.markdown
parentbbe6d059326ce3c8ff0d337fcb87f65dcfebe27a (diff)
Python: add finally and with statements
https://docs.python.org/2/tutorial/errors.html
Diffstat (limited to 'python.html.markdown')
-rw-r--r--python.html.markdown6
1 files changed, 6 insertions, 0 deletions
diff --git a/python.html.markdown b/python.html.markdown
index 88e0deb1..9493e6a3 100644
--- a/python.html.markdown
+++ b/python.html.markdown
@@ -379,7 +379,13 @@ except (TypeError, NameError):
pass # Multiple exceptions can be handled together, if required.
else: # Optional clause to the try/except block. Must follow all except blocks
print "All good!" # Runs only if the code in try raises no exceptions
+finally: # Execute under all circumstances
+ print "We can clean up resources here"
+# Instead of try/finally to cleanup resources you can use with
+with open("myfile.txt") as f:
+ for line in f:
+ print line
####################################################
## 4. Functions