Python Lecture 4
Python Lecture 4
n Today:
n More odds and ends
n assertions
n "print >>" syntax
n more on argument lists
n functional programming tools
n list comprehensions
n More on exception handling
n More on object-oriented programming
n inheritance, multiple inheritance, etc.
Odds and ends (1)
n Assertions
# 'i' should be zero here:
assert i == 0
# If fail, exception raised.
sys.stderr.write("bad!\n")
n Either is fine
n Note that write() doesn't add newline
at end
Odds and ends (2) – arg lists
n Default arguments, keyword arguments
def foo(val=10):
print val
foo() # prints 10
foo(20) # prints 20
foo(val=30) # prints 30
def foo(x):
return x * 2
>>> bar = foo
>>> bar(3)
6
Functional programming tools (2)
n lambda, map, reduce, filter:
>>> map(lambda x: x * 2, [1, 2, 3, 4, 5])
[2, 4, 6, 8, 10]
>>> reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
15
>>> sum([1, 2, 3, 4, 5]) # easier
15
>>> filter(lambda x: x % 2 == 1, range(10))
[1, 3, 5, 7, 9]
List comprehensions
>>> vec = [2, 4, 6]
>>> [3 * x for x in vec]
[6, 12, 18]
>>> [3 * x for x in vec if x > 3]
[12, 18]
>>> [3 * x for x in vec if x < 2]
[]
>>> [[x, x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
try/finally (1)
n We put code that can raise exceptions into a try
block
n We catch exceptions inside except blocks
n We don't have to catch all exceptions
n If we don't catch an exception, it will leave the function
and go to the function that called that function, until it
finds a matching except block or reaches the top level
n Sometimes, we need to do something regardless of
whether or not an exception gets thrown
n e.g. closing a file that was opened in a try block
try/finally (2)
try:
# code goes here...
if something_bad_happens():
raise MyException("bad")
finally:
# executes if MyException was not raised
# executes and re-raises exception
# if MyException was raised
try/finally (3)
n Typical example of try/finally use:
try:
myfile = file("foo") # open file "foo"
if something_bad_happens():
raise MyException("bad")
finally:
# Close the file whether or not an
# exception was thrown.
myfile.close()
# If an exception was raised, python
# will automatically reraise it here.
try/finally (4)
n Execution profile:
try:
# ... code (1) ...
finally:
# ... code (2) ...
# ... code (3) ...
n When no exception raised: (1) then (2) then (3)
n When exception raised: (1) then (2) then exit
function
try/finally (5)
nThis is also legal:
try:
# code that can raise exceptions
except SomeException, e:
# code to handle exceptions
finally:
# code to execute whether or not
# an exception was raised
try/finally (6)
try:
# ... code (1) ...
except:
# ... code (2) ...
finally:
# ... code (3) ...
# ... code (4) ...
n When no exception raised: (1) then (3) then (4)
n When exception raised and caught: (1) then (2)
then (3) then (4)
n When exception raised but not caught: (1) then (3)
then exit function
Exception classes
n Exception classes, with arguments:
class MyException:
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
try:
raise MyException(42)
except MyException, e:
print "bad! value: %d" % e.value
More on OOP -- inheritance
n Often want to create a class which is a
specialization of a previously-existing class
n Don't want to redefine the entire class from scratch
n Just want to add a few new methods and fields
n To do this, the new class can inherit from another
class; this is called inheritance
n The class being inherited from is called the parent
class, base class, or superclass
n The class inheriting is called the child class, derived
class, or subclass
Inheritance (2)
n Inheritance:
class SubClass(SuperClass):
<statement-1>
...
<statement-N>
n Or:
class SubClass(mod.SuperClass):
# ...
n if SubClass is defined in another module
Inheritance (3)
n Name resolution:
foo = Foo() # instance of class Foo
foo.bar()
n If bar method not in class Foo
n superclass of Foo searched
n etc. until bar found or top reached
n AttributeError raised if not found
n Same thing with fields (foo.x)
Inheritance (4)
n Constructors:
n Calling __init__ method on subclass
doesn't automatically call superclass
constructor!
n Can call superclass constructor explicitly if
necessary
Inheritance (5)
class Super:
def __init__(self, x):
self.x = x
class Sub(Super):
def __init__(self, y):
Super.__init__(self, y)
self.y = y
Inheritance example (1)
class Animal:
def __init__(self, weight):
self.weight = weight
def eat(self):
print "I am eating!"
def __str__(self):
return "Animal; weight = %d" % \
self.weight
Inheritance example (2)
>>> a = Animal(100)
>>> a.eat()
I am eating!
>>> a.weight
100
>>> a.fly()
AttributeError: Animal instance has no
attribute 'fly'
Inheritance example (3)
class Bird(Animal):
def fly(self):
print "I am flying!"
b = Bird(100) # Animal's __init__() method
b.eat()
I am eating!
b.fly()
I am flying!
Exceptions again
n Can use inheritance to make it easy to
generate simple exception subclasses:
class MyException(Exception):
pass
n __spam à _<classname>__spam
n <classname> is current class name
n Weak form of privacy protection
Next week
n We'll talk about the new features and
changes in Python 3.x (3.0, 3.1, etc.)