![Page 1: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/1.jpg)
CME 193: Introduction to Scientific Python
Lecture 7: Recursion, Exception handling
Sven Schmit
stanford.edu/~schmit/cme193
7: Recursion, Exception handling 7-1
![Page 2: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/2.jpg)
Contents
Recursion
Exception handling
Exercises
7: Recursion, Exception handling 7-2
![Page 3: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/3.jpg)
Back to control flow
To execute repetitive code, we have relied on for and while loops.
Furthermore, we used if statements to handle conditional statements.
These statements are rather straightforward and easy to understand.
7: Recursion, Exception handling 7-3
![Page 4: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/4.jpg)
Recursion
Recursive function solve problems by reducing them to smaller problems
of the same form.
This allows recursive functions to call themselves...
New paradigm
Powerful tool
Divide-and-conquer
Beautiful solutions
7: Recursion, Exception handling 7-4
![Page 5: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/5.jpg)
Recursion
Recursive function solve problems by reducing them to smaller problems
of the same form.
This allows recursive functions to call themselves...
New paradigm
Powerful tool
Divide-and-conquer
Beautiful solutions
7: Recursion, Exception handling 7-5
![Page 6: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/6.jpg)
First example
Let’s consider a trivial problem:
Suppose we want to add two positive numbers a and b, but we can only
add/subtract one.
7: Recursion, Exception handling 7-6
![Page 7: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/7.jpg)
First example
Non-recursive solution:
def add(a, b):while b > 0:
a += 1b -= 1
return a
7: Recursion, Exception handling 7-7
![Page 8: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/8.jpg)
First example
Recursive solution:
Simple case: b = 0, return a
Else, we can return 1 + add(a, b-1)
def add(a, b):if b == 0:
# base casereturn a
# recursive stepreturn add(a, b-1) + 1
7: Recursion, Exception handling 7-8
![Page 9: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/9.jpg)
First example
Recursive solution:
Simple case: b = 0, return a
Else, we can return 1 + add(a, b-1)
def add(a, b):if b == 0:
# base casereturn a
# recursive stepreturn add(a, b-1) + 1
7: Recursion, Exception handling 7-9
![Page 10: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/10.jpg)
Base case and recursive steps
Recursive functions consist of two parts:
base case The base case is the trivial case that can be dealt with
easily.
recursive step The recursive step brings us slightly closer to the base
case and calls the function itself again.
7: Recursion, Exception handling 7-10
![Page 11: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/11.jpg)
Reversing a list
How can we recursively reverse a list ([1, 2, 3] → [3, 2, 1]).
If list is empty or has one element, the reverse is itself
Otherwise, reverse elements 2 to n, and append the first
def reverse_list(xs):if len(xs) <= 1:
return xselse:
return reverse_list(xs[1:]) + [xs[0]]
7: Recursion, Exception handling 7-11
![Page 12: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/12.jpg)
Reversing a list
How can we recursively reverse a list ([1, 2, 3] → [3, 2, 1]).
If list is empty or has one element, the reverse is itself
Otherwise, reverse elements 2 to n, and append the first
def reverse_list(xs):if len(xs) <= 1:
return xselse:
return reverse_list(xs[1:]) + [xs[0]]
7: Recursion, Exception handling 7-12
![Page 13: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/13.jpg)
Palindromes
A palindrome is a word that reads the same from both ways, such as
radar or level.
Let’s write a function that checks whether a given word is a palindrome.
7: Recursion, Exception handling 7-13
![Page 14: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/14.jpg)
The recursive idea
Given a word, such as level, we check:
whether the first and last character are the same
whether the string with first and last character removed are the same
7: Recursion, Exception handling 7-14
![Page 15: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/15.jpg)
Base case
What’s the base case in this case?
The empty string is a palindrome
Any 1 letter string is a palindrome
7: Recursion, Exception handling 7-15
![Page 16: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/16.jpg)
Base case
What’s the base case in this case?
The empty string is a palindrome
Any 1 letter string is a palindrome
7: Recursion, Exception handling 7-16
![Page 17: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/17.jpg)
Implementation
def is_palin(s):’’’returns True iff s is a palindrome’’’if len(s) <= 1:
return Truereturn s[0] == s[-1] and is_palin(s[1:-1])
What is an iterative solution?
7: Recursion, Exception handling 7-17
![Page 18: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/18.jpg)
Implementation
def is_palin(s):’’’returns True iff s is a palindrome’’’if len(s) <= 1:
return Truereturn s[0] == s[-1] and is_palin(s[1:-1])
What is an iterative solution?
7: Recursion, Exception handling 7-18
![Page 19: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/19.jpg)
Numerical integration
Suppose we want to numerically integrate some function f :
A =∫ b
af (x)dx
Trapezoid rule:
A =∫ b
af (x)dx
=∫ r1
af (x)dx +
∫ r2
r1
f (x)dx + . . . +∫ b
rn−1
f (x)dx
≈ h2 ((f (a) + f (r1)) + h(f (r1) + f (r2)) + . . . + (f (b) + f (rn−1)))
= h2 (f (a) + f (b)) + h(f (r1) + f (r2) + . . . + f (rn−1))
7: Recursion, Exception handling 7-19
![Page 20: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/20.jpg)
Numerical integration
Suppose we want to numerically integrate some function f :
A =∫ b
af (x)dx
Trapezoid rule:
A =∫ b
af (x)dx
=∫ r1
af (x)dx +
∫ r2
r1
f (x)dx + . . . +∫ b
rn−1
f (x)dx
≈ h2 ((f (a) + f (r1)) + h(f (r1) + f (r2)) + . . . + (f (b) + f (rn−1)))
= h2 (f (a) + f (b)) + h(f (r1) + f (r2) + . . . + f (rn−1))
7: Recursion, Exception handling 7-20
![Page 21: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/21.jpg)
Trapezoid rule
def trapezoid(f, a, b, N):’’’integrates f over [a,b] using N steps’’’if a > b:
a, b = b, a# step sizeh = float(b-a)/N# running sums = h/2 * (f(a) + f(b))for k in xrange(1, N-1):
s += h * f(a + h*k)return s
Forget the math / code: This function approximates the area under f
between a and b using N points.
7: Recursion, Exception handling 7-21
![Page 22: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/22.jpg)
Key point
If function is flat, then we don’t need many points.
If function is very wiggly, we need a lot of points.
So:
How many points do we need?
What if function is flat in some areas, wiggly in others?
7: Recursion, Exception handling 7-22
![Page 23: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/23.jpg)
Adaptive integration
Idea: Adaptively space points based on local curvature of function.
Areas where function is flat: few points, areas where function is wiggly:
many points.
def ada_int(f, a, b, tol=1.0e-6, n=5, N=10):area = trapezoid(f, a, b, N)check = trapezoid(f, a, b, n)if abs(area - check) > tol:
# bad accuracy, add more points to intervalm = (b + a) / 2.0area = ada_int(f, a, m) + ada_int(f, m, b)
return area
Note: we do not need to use trapezoid rule.
7: Recursion, Exception handling 7-23
![Page 24: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/24.jpg)
Adaptive integration
Idea: Adaptively space points based on local curvature of function.
Areas where function is flat: few points, areas where function is wiggly:
many points.
def ada_int(f, a, b, tol=1.0e-6, n=5, N=10):area = trapezoid(f, a, b, N)check = trapezoid(f, a, b, n)if abs(area - check) > tol:
# bad accuracy, add more points to intervalm = (b + a) / 2.0area = ada_int(f, a, m) + ada_int(f, m, b)
return area
Note: we do not need to use trapezoid rule.
7: Recursion, Exception handling 7-24
![Page 25: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/25.jpg)
Pitfalls
Recursion can be very powerful, but there are some pitfalls:
Have to ensure you always reach the base case.
Each successive call of the algorithm must be solving a simpler
problem
The number of function calls shouldn’t explode. (see exercises)
An iterative algorithm is always faster due to overhead of function
calls. (However, the iterative solution might be much more complex)
7: Recursion, Exception handling 7-25
![Page 26: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/26.jpg)
Contents
Recursion
Exception handling
Exercises
7: Recursion, Exception handling 7-26
![Page 27: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/27.jpg)
Wordcount
Recall the wordcount exercise:
Write a function that takes a filename, and returns the 20 most common
words.
7: Recursion, Exception handling 7-27
![Page 28: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/28.jpg)
Suppose we have written a function topkwords(filename, k)
Instead of entering filename and value of k in the script, we can also run
it from the terminal.
7: Recursion, Exception handling 7-28
![Page 29: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/29.jpg)
Parse input from command line
The sys module allows us to read the terminal command that started
the script:
import sys
print sys.argv
sys.argv holds a list with command line arguments passed to a Python
script.
7: Recursion, Exception handling 7-29
![Page 30: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/30.jpg)
Parse input from command line
The sys module allows us to read the terminal command that started
the script:
import sys
print sys.argv
sys.argv holds a list with command line arguments passed to a Python
script.
7: Recursion, Exception handling 7-30
![Page 31: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/31.jpg)
Back to the wordcount example
import sys
def topkwords(filename, k):# Returns k most common words in filenamepass
if __name__ == "__main__":filename = sys.argv[1]k = int(sys.argv[2])print topkwords(filename, k)
Issues?
7: Recursion, Exception handling 7-31
![Page 32: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/32.jpg)
Back to the wordcount example
import sys
def topkwords(filename, k):# Returns k most common words in filenamepass
if __name__ == "__main__":filename = sys.argv[1]k = int(sys.argv[2])print topkwords(filename, k)
Issues?
7: Recursion, Exception handling 7-32
![Page 33: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/33.jpg)
Issues
What if the file does not exist?
What if the second argument is not an integer?
What if no command line arguments are supplied?
All result in errors:
IOError
ValueError
IndexError
7: Recursion, Exception handling 7-33
![Page 34: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/34.jpg)
Issues
What if the file does not exist?
What if the second argument is not an integer?
What if no command line arguments are supplied?
All result in errors:
IOError
ValueError
IndexError
7: Recursion, Exception handling 7-34
![Page 35: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/35.jpg)
Issues
What if the file does not exist?
What if the second argument is not an integer?
What if no command line arguments are supplied?
All result in errors:
IOError
ValueError
IndexError
7: Recursion, Exception handling 7-35
![Page 36: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/36.jpg)
Exception handling
What do we want to happen when these errors occur? Should the
program simply crash?
No, we want it to gracefully handle these
IOError: Tell the user the file does not exist.
ValueError, IndexError: Tell the user what the format of the
command line arguments should be.
7: Recursion, Exception handling 7-36
![Page 37: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/37.jpg)
Exception handling
What do we want to happen when these errors occur? Should the
program simply crash?
No, we want it to gracefully handle these
IOError: Tell the user the file does not exist.
ValueError, IndexError: Tell the user what the format of the
command line arguments should be.
7: Recursion, Exception handling 7-37
![Page 38: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/38.jpg)
Try ... Except
import sys
if __name__ == "__main__":try:
filename = sys.argv[1]k = int(sys.argv[2])print topkwords(filename, k)
except IOError:print "File does not exist"
except (ValueError, IndexError):print "Error in command line input"print "Run as: python wc.py <filename> <k>"print "where <k> is an integer"
7: Recursion, Exception handling 7-38
![Page 39: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/39.jpg)
Try ... Except
The try clause is executed
If no exception occurs, the except clause is skipped
If an exception occurs, the rest of the try clause is skipped. Then if
the exception type is matched, the except clause is executed. Then
the code continues after the try statement
If an exception occurs with no match in the except clause, execution
is stopped and we get the standard error
7: Recursion, Exception handling 7-39
![Page 40: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/40.jpg)
Try ... Except
The try clause is executed
If no exception occurs, the except clause is skipped
If an exception occurs, the rest of the try clause is skipped. Then if
the exception type is matched, the except clause is executed. Then
the code continues after the try statement
If an exception occurs with no match in the except clause, execution
is stopped and we get the standard error
7: Recursion, Exception handling 7-40
![Page 41: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/41.jpg)
Try ... Except
The try clause is executed
If no exception occurs, the except clause is skipped
If an exception occurs, the rest of the try clause is skipped. Then if
the exception type is matched, the except clause is executed. Then
the code continues after the try statement
If an exception occurs with no match in the except clause, execution
is stopped and we get the standard error
7: Recursion, Exception handling 7-41
![Page 42: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/42.jpg)
A naked except
We can have a naked except that catches any error:
try:t = 3.0 / 0.0
except:# handles any errorprint ’There was some error’
Use this with extreme caution though, as genuine bugs might be
impossible to correct!
7: Recursion, Exception handling 7-42
![Page 43: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/43.jpg)
Try Except Else
Else clause after all except statements is executed after successful
execution of the try block (hence, when no exception was raised)
for arg in sys.argv[1:]:try:
f = open(arg, ’r’)except IOError:
print ’cannot open’, argelse:
print arg, ’has’, len(f.readlines()), ’lines’f.close()
# from Python docs
Why? Avoids catching exception that was not protected
E.g. consider f.readlines raising an IOError
7: Recursion, Exception handling 7-43
![Page 44: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/44.jpg)
Raise
We can use Raise to raise an exception ourselves.
>>> raise NameError(’Oops’)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: Oops
7: Recursion, Exception handling 7-44
![Page 45: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/45.jpg)
Finally
The finally statement is always executed before leaving the try
statement, whether or not an exception has occured.
def div(x, y):try:
return x/yexcept ZeroDivisionError:
print ’Division by zero!’finally:
print "Finally clause"
print div(3,2)print div(3,0)
Useful in case we have to close files, closing network connections etc.
7: Recursion, Exception handling 7-45
![Page 46: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/46.jpg)
Finally
The finally statement is always executed before leaving the try
statement, whether or not an exception has occured.
def div(x, y):try:
return x/yexcept ZeroDivisionError:
print ’Division by zero!’finally:
print "Finally clause"
print div(3,2)print div(3,0)
Useful in case we have to close files, closing network connections etc.
7: Recursion, Exception handling 7-46
![Page 47: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/47.jpg)
Raising our own exceptions: Rational class
Recall the Rational class we considered a few lectures ago:
class Rational:def __init__(self, p, q=1):
g = gcd(p, q)self.p = p / gself.q = q / g
What if q = 0?
7: Recursion, Exception handling 7-47
![Page 48: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/48.jpg)
Rational class
a = Rational(1,3)b = Rational(2,3)c = Rational(2,0)
print ’a*b = {}’.format(a*b) # 2/9print ’a*c = {}’.format(a*c) # 1/0print ’a/b = {}’.format(a/b) # 1/2print ’a/c = {}’.format(a/c) # 0print ’c/a = {}’.format(c/a) # 1/0
... Not really anything!
Rational class: https://gist.github.com/schmit/875840d1a231526b572e
7: Recursion, Exception handling 7-48
![Page 49: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/49.jpg)
Making the necessary change
If q = 0, then raise an exception!
Which one?
class Rational:def __init__(self, p, q=1):
if q == 0:raise ZeroDivisionError(’denominator is zero’)
g = gcd(p, q)self.p = p / gself.q = q / g
In the above example, where is the exception raised?
7: Recursion, Exception handling 7-49
![Page 50: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/50.jpg)
Making the necessary change
If q = 0, then raise an exception!
Which one?
class Rational:def __init__(self, p, q=1):
if q == 0:raise ZeroDivisionError(’denominator is zero’)
g = gcd(p, q)self.p = p / gself.q = q / g
In the above example, where is the exception raised?
7: Recursion, Exception handling 7-50
![Page 51: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/51.jpg)
Contents
Recursion
Exception handling
Exercises
7: Recursion, Exception handling 7-51
![Page 52: CME193: IntroductiontoScientificPython Lecture7: Recursion ...stanford.edu/~schmit/cme193/lec/lec7.pdf · Backtocontrolflow Toexecuterepetitivecode,wehavereliedonfor andwhile loops](https://reader034.vdocuments.us/reader034/viewer/2022052612/5f0a014f7e708231d4298d37/html5/thumbnails/52.jpg)
Exercises
See course website for exercises for this week.
Get to know the person next to you and do them in pairs!
Let me know if you have any question
Class ends at 5:35pm.
7: Recursion, Exception handling 7-52