summaryrefslogtreecommitdiffhomepage
path: root/python3.html.markdown
diff options
context:
space:
mode:
authorDenis Gladkikh <denis@gladkikh.email>2015-09-01 10:25:04 -0700
committerDenis Gladkikh <denis@gladkikh.email>2015-09-01 10:25:04 -0700
commit1b767166e112aa4cbd9758ac722f2286b534803f (patch)
tree4db31506a9d7a337009406a505e361318fb551b7 /python3.html.markdown
parentbbe6d059326ce3c8ff0d337fcb87f65dcfebe27a (diff)
Python3: add finally and with statements
Diffstat (limited to 'python3.html.markdown')
-rw-r--r--python3.html.markdown7
1 files changed, 7 insertions, 0 deletions
diff --git a/python3.html.markdown b/python3.html.markdown
index 9d965fb1..fbed77fe 100644
--- a/python3.html.markdown
+++ b/python3.html.markdown
@@ -374,6 +374,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
# Python offers a fundamental abstraction called the Iterable.
# An iterable is an object that can be treated as a sequence.