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), [ ] ]

For example, suppose that the following are true:
  • the line at time 37 = 6
  • the tab at time 37 = 2
  • statement at line 6, tab 1 = def age_in_months(self):
  • Control Map at time 37 = [ entry 0: (pair ("class", "Person")), [ entry 1: (pair ("method", "__init__")), [ ] ] ]
  • Variables Map at time 37 = [ entry self: (Python reference 0), [ entry first_name: "John", [ entry last_name: "Smith", [ entry age: 25, [ ] ] ] ] ]

Here is the "Variables" map in table form:
NameValue
selfpy_ref(0)
first_name"John"
last_name"Smith"
age25

We know that 1 < 2. Also, we can see that:

value at (2 - 1) in map (Control Map at time 37) = pair ("method", "__init__")

Therefore, we can conclude that:

Return Value at time (37 + 1) = value at self in map (Variables Map at time 37)

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 37) = Python reference 0

Thus, we conclude that:

Return Value at time (37 + 1) = [ Python reference 0, [ ] ]

Which simplifies to:

Return Value at time 38 = [ Python reference 0, [ ] ]

See here for the full proof.

Try stepping through the simulator to see LW Python push reference 0 to the Return Value

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments