From a24e5ef7a115eb32e929a003fdfcdb3704927064 Mon Sep 17 00:00:00 2001 From: Denis Gladkikh Date: Tue, 1 Sep 2015 09:27:40 -0700 Subject: Python: add finally and with statements https://docs.python.org/2/tutorial/errors.html --- python.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'python.html.markdown') 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 -- cgit v1.2.3 From 3e93c5e5f4a4f968a2371dc0b69047bc78da4640 Mon Sep 17 00:00:00 2001 From: Denis Gladkikh Date: Tue, 1 Sep 2015 10:23:31 -0700 Subject: Update python.html.markdown --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 9493e6a3..16a94c8f 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -382,7 +382,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks finally: # Execute under all circumstances print "We can clean up resources here" -# Instead of try/finally to cleanup resources you can use with +# 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 -- cgit v1.2.3