summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGeoff Liu <cangming.liu@gmail.com>2015-09-01 11:52:02 -0600
committerGeoff Liu <cangming.liu@gmail.com>2015-09-01 11:52:02 -0600
commit153cd4eeaf5a659cb34adfd08291144a1144e9cb (patch)
tree384c1c6c39ba4e8e8d5e2217fb6acdb870c397d0
parentbbe6d059326ce3c8ff0d337fcb87f65dcfebe27a (diff)
parent3e93c5e5f4a4f968a2371dc0b69047bc78da4640 (diff)
Merge pull request #1222 from outcoldman/patch-1
Python: add finally and with statements
-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..16a94c8f 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 a with statement
+with open("myfile.txt") as f:
+ for line in f:
+ print line
####################################################
## 4. Functions