You are on page 1of 19

Review 1

Call Frames;
Diagramming Objects

The Big Issue

•  Cannot answer questions on this topic unless you



§  draw variables

§  draw frames for function calls

§  draw objects when they are created

•  Learning to do this is useful in general



§  Helps you “think like a computer”

§  Easier to find errors in your programs.

12/2/12
Review 1
2

What Do You Need to Know?

•  Major topics
§  local variables (in a function body)
§  function call (call frames, call stack)
§  constructor call (in addition to call frames )

•  Examples from previous exams


§  Question 5 on prelim 1
§  Questions 3 and 5b on prelim 2

12/2/12
Review 1
3

Important

•  Code execution is an important part of the final


•  You need to know how to
§  draw variables
§  draw call frames
§  draw objects
The purpose of such questions on executing
statements with constructs and function calls is
to test your understanding of how Python
programs are executed
12/2/12
Review 1
4

The Frame (box) for a Function Call

•  Function Frame: Representation of function call

•  A conceptual model of Python

Draw parameters • Number of statement in the
as variables  function body to execute next
(named boxes)
• Starts with 1

function name instruction counter


parameters
local variables (when assigned)

12/2/12
Review 1
5

To Execute the Method: x.addScore(100)
1.  Draw a frame for the call
addScore
1

2.  Assign the arguments to the
parameters (in frame)
self 1257981

3.  Execute the method body
value
100
§  Look for variables in frame

§  If an attribute, follow the
name into Heap Space
x 1257981
4.  Erase the frame
1257981
class Score(object): Score
_score = 0
score 10

def addScore(self,value): __init__(value)  
self._score = self._score+value
12/2/12
Review 1
addScore(value)   6

To Execute the Method: x.addScore(100)
1.  Draw a frame for the call
addScore

2.  Assign the arguments to the
parameters (in frame)
self 1257981

3.  Execute the method body
value
100
§  Look for variables in frame

§  If an attribute, follow the
name into Heap Space
x 1257981
4.  Erase the frame
1257981
class Score(object): Score
_score = 0
score 10 110



def addScore(self,value): __init__(value)  
self._score = self._score+value
12/2/12
Review 1
addScore(value)   7

Call Stacks: Given a Line to Reach

def last_name_first(s):
last_name_first 2
"""Precondition: s in the form"
<first-name> <last-name>""" " s 'Walker White'
1 first = first_name(s)
2 last = last_name(s) first 'Walker'

3 return last + '.' + first " last



def last_name(s): last_name 2
"""Prec: see last_name_first"""
1 end = s.find(' ') s 'Walker White'
2 return s[end+1:] Execute to here

end 6


12/2/12
Review 1
8

(Modified) Question from Previous Years

def reverse(b):
•  Execute the call

"""Reverse elements of b in place "
(does not make a copy) §  a = [5,7,3]; reverse(a)
Pre: b is a list""" §  Use ‘folder’ for list a below

1 reverse_part(b,0,len(b)-1) §  Stop at (before) line 2

§  Draw call frame at that time!

def reverse_part(b,h,k):
"""Reverse b[h..k] in place
a 23457811 23457811
Pre: b is a list; h, k are in b"""
1 if h >= k: a[0] 5
2 return Give only one frame per call a[1] 7
3 temp = b[h] a[2] 3
Give the state of the frame at
4 b[h] = b[k] last line before call terminates
5 b[k] = temp
6 reverse_part(b,h+1,k-1)
12/2/12
Review 1
9

Execute the Call reverse([5,7,3]) to Line 2

def reverse(b): reverse 1 a 23457811
"""Reverse elements of b in place "
(does not make a copy) b 23457811 23457811
Pre: b is a list"""
1 reverse_part(b,0,len(b)-1)
a[0] ✗

5 3
reverse_part 6 a[1] 7

def reverse_part(b,h,k): b 23457811 h 0
a[2] ✗

3 5
"""Reverse b[h..k] in place
k 2 temp 5
Pre: b is a list; h, k are in b"""
1 if h >= k:
2 return reverse_part 2
3 temp = b[h]
4 b[h] = b[k]
b 23457811
5 b[k] = temp h 1 k 1
6 reverse_part(b,h+1,k-1)
12/2/12
Review 1
10

Diagramming Objects (Folders)

Folder Name

(make it up)

4300517584

classname
Attributes: Draw attributes as
• Unhidden fields named box w/ value
• Properties with a getter
(even for properties)

Methods:
• Name+parameter names Include operators here

• But do not include self (but only if defined)

12/2/12
Review 1
11

Diagramming Example

class Time(object):
hr = 0 # Hour of day 9127451
_min = 0 # Minute of hour
Time  
@property # getter only
def min(self): hr 2 Unhidden field

return self._min
min 30 Property

def __init__(self, h, m=0):
"""Constructor: new time h:m"""
self._hr = h; self._min = m __init__(h,m=0)

def __str__(self): __str__()


"""Returns string '<hr>:<min>' """
return `self._hr` + ':' + `self._min`

12/2/12
Review 1
12

Evaluation of a Constructor Call

3 steps to evaluating the call C(args)


•  Create a new folder (object) of class C
§  Give it with a unique name (any number will do)
§  Folder goes into heap space
•  Execute the method __init__(args)
•  Yield the name of the object as the value
§  A constructor call is an expression, not a command
§  Does not put name in a variable unless you assign it

12/2/12
Review 1
13

Code Segment (with Constructors)

a = 3
First thing to do?
x = C(a) # C is a class
draw all of the
y = C(a) local variables
x = y

12/2/12
Review 1
14

Code Segment (with Constructors)

class C(object):
f = 0 x y a 3
def __init__(self, k):
self.f = k

a = 3
x = C(a) # C a class
y = C(a)
x = y
12/2/12
Review 1
15

Code Segment (with Constructors)

class C(object):
x y a


f = 0 4896 9127 3
def __init__(self, k):
9127
self.f = k

4896 9127
a = 3 C C
f 3 f 3
x = C(a) # C a class
__init__(k) __init__(k)
y = C(a)
x = y aliasing  
12/2/12
Review 1
16

Code Execution (Q4 from 2008 fall final, modified)

Execute the call: class Item(object):


_cost = 0 # cost of this item
session()
_name = '' # Item name

def __init__(self, t, c):


def session() """Constructor: new Item with name t, cost c"""
1 one = Item('ipod', 20) self._name = t; self._cost = c

2 two = Item('wii', 32) @property # getter only


3 treat = two def cost(self):
"""The cost of this item """
4 three = one
return self._cost
5 three.add(4)

@property # getter only


6 print one def name(self):
7 print 'Cost of item one: '+str(one.cost) """This item’s name"""
return self._name
8 print ('Are they the same? ' +

def __str__(self):
str(one.name== two.name))
"""Returns '<name>:<cost>' as representation"""
9 print ('Are they the same? ' + return self.name + ':' + str(self.cost)
str(one.name== treat.name))

def add(self, d):


10 print ('Are they the same? ' + """Add d to this item’s cost"""
str(one.name== three.name)) self._cost = self._cost + d
Code Execution (Q4 from 2008 fall final, modified)

Execute the call: one 4896 two 9127


session()
treat 9127 three 4896
def session()
1 one = Item('ipod', 20)
2 two = Item('wii', 32) 4896 9127
3 treat = two
4 three = one Item Item
5 three.add(4) cost cost
20 32
6 print one
7 print 'Cost of item one: '+str(one.cost) name 'ipod'   name 'wii'  
8 print ('Are they the same? ' +
str(one.name== two.name)) __init__(t,c) __init__(t,c)
9 print ('Are they the same? ' +
str(one.name== treat.name)) __str__() __str__()
10 print ('Are they the same? ' + add(d) add(d)
str(one.name== three.name))
Code Execution (Q4 from 2008 fall final, modified)

Execute the call: one 4896 two 9127


session()
treat 9127 three 4896
def session()
1 one = Item('ipod', 20)
2 two = Item('wii', 32)
3 treat = two Output:
4 three = one
5 three.add(4)
6 : 'ipod:24'
6 print one
7 : 'Cost of item one: 24'
7 print 'Cost of item one: '+str(one.cost)
8 print ('Are they the same? ' + 8 : 'Are they the same? False'
str(one.name== two.name))
9 print ('Are they the same? ' + 9 : 'Are they the same? False'
str(one.name== treat.name))
10 print ('Are they the same? ' + 10 : 'Are they the same? True'
str(one.name== three.name))

You might also like