## Suggested Homework #5 (for Quiz #5 on 02/28/07)

Problem 1: Suppose that the following was entered into the Python shell. For each entry, the interpreter responds with either: (a) the value of the object computed, (b) nothing, if it is an assignment (name binding), or the value computed is None , or, (c) an error message due to an exception. Fill in the blanks ("___") with how the Python interpreter would respond if each of the below was entered in the sequence shown. Note that you could just enter this sequence into the interpreter and see what happens, but this is a luxury you will not have during the quiz. ``` ```
``````>>> 2 + 4.2
________

>>> "this " + "is"*4 + "easy"
________

>>> (1,2.2,"a") + (3,4.5,"b")
________

>>> z = ("a",3) * 5
________

>>> q = z[3][0] * z[2][1]
________

>>> y = [1,2,"3",[4,6]] + ["a","b","c"]
________

>>> y[2:6]
________

>>> y[3:]
________

>>> y[3][:]
________

>>> i,p = 1,1
while (i<5):
i,p = i+1,p*i

p
________

>>> r = range(5,15)
r
________

>>> for i in r:
p += i

p
________

>>> def f(x):
if x:
return x+a
________

>>> f(0)
________

>>> f(10)
________

>>> f("")
________

>>> f("a")
________

>>> p = q
________

>>> f(0)
________

>>> f(10)
________

>>> f("")
________

>>> f("a")
________
``````
Problem 2: Call-by-value. Repeat the above for the next few entries (again entered in sequence).
``````
>>> def foo1(x,y):
x = x + y

>>> def foo2(x,y):
x[len(x):] = y

>>> p = [1,2,3,4]

>>> foo1(p,"abc")

p
________

>>> foo2(p,"abc")

p
________

>>> q = ["a","b","c"]

>>> foo1(q,[1,2,3])

q
________

>>> foo2(q,[1,2,3])

>>> q

________

``````
``` ``` Problem 3:

Consider the Python code below:

``````
class Count:
ctr = 0
def __init__(self,init,fnext):
self.ctr = init
self.n = fnext

def nextValue(self):
return self.n(self.ctr)

def next(self):
self.ctr = self.nextValue()
return self.ctr

class DCount(Count):
def nextValue(self):
return self.n(self.n(self.ctr))

c = Count(1,lambda x: x + 1)
d = DCount(1,lambda x: 2*x)
c.next()
d.next()
f = d.next

r1 = DCount.ctr
r2 = c.ctr
r3 = d.ctr
r4 = f()
r5 = Count.next(d)

``````
(a) What are the attributes of the namespace `Count` ?
(b) What are the attributes of the namespace `DCount` ?
(c) Which class is the super class, which class is the subclass ?
(d) Which methods does the subclass inherit from the superclass ?
(e) Which methods of the superclass get overridden by the subclass ?
(f) What are the values of ` r1,r2,r3,r4,r5 ` ?