Professional Documents
Culture Documents
Call Frames;
Diagramming Objects
The Big Issue
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 )
12/2/12
Review 1
3
Important
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'
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)
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)
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))