Return from Constructor Body
When Python reaches the end of the constructor body, set the return value to the reference to "self." More formally,
if the following are true:
- the line at time t = i
- the tab at time t = j
- statement at line i, tab k = s
- k < j
- value at (j - 1) in map (Control Map at time t) = pair ("method", "__init__")
then Return Value at time (t + 1) = value at self in map (Variables Map at time t)
- the line at time 50 = 6
- the tab at time 50 = 2
- statement at line 6, tab 1 =
def age_in_months(self):
- Control Map at time 50 = [ entry 0: (pair ("class", "Person")), [ entry 1: (pair ("method", "__init__")), [ ] ] ]
- Variables Map at time 50 = [ entry self: (Python reference 0), [ entry first_name: "John", [ entry last_name: "Smith", [ entry age: 25, [ ] ] ] ] ]
Here is the "Variables" map in table form:
Name | Value |
---|---|
self | py_ref(0) |
first_name | "John" |
last_name | "Smith" |
age | 25 |
We know that 1 < 2. Also, we can see that:
value at (2 - 1) in map (Control Map at time 50) = pair ("method", "__init__")
Therefore, we can conclude that:
Return Value at time (50 + 1) = value at self in map (Variables Map at time 50)
Since:
value at self in map [ entry self: (Python reference 0), [ entry first_name: "John", [ entry last_name: "Smith", [ entry age: 25, [ ] ] ] ] ] = Python reference 0
We claim that:
value at self in map (Variables Map at time 50) = Python reference 0
Thus, we conclude that:
Return Value at time (50 + 1) = Python reference 0
Which simplifies to:
Return Value at time 51 = Python reference 0
See here for the full proof.
Try stepping through the simulator to see LW Python set the "return value" to reference 0
Current Line | 1 | Current Tab | 0 | Time | 0 |
Comments
Please log in to add comments