List Set Item Property

This property describes how LW Python sets (updates) a list element at a given index. First, the top element in the "Values" stack tells us the following:

  • The new value.
  • The list index that should be updated.
  • Where the list is in the "Object Store."

LW Python updates the list with the new value and then replaces the old list in the "Object Store" with the new list.

More formally,

if the following are true:
  • expression state at time t = "call_function_begin"
  • Value Stack at time t = [ [ value, [ key, [ Python reference idx, [ ] ] ] ], stack ]
  • the expression at time t = function call with name: "__setitem__" and arguments: [ obj, rest ]
  • the element at index idx of stack (Python Object Store at time t) = Python list elements

then Python Object Store at time (t + 1) = result of storing (Python list (result of storing value at index key of stack elements)) at index idx of stack (Python Object Store at time t)

For example, suppose that the following are true:

  • expression state at time 26 = "call_function_begin"
  • Value Stack at time 26 = [ [ 9, [ 1, [ Python reference 0, [ ] ] ] ], [ ] ]
  • the expression at time 26 = __setitem__(numbers, 1, 9)
  • Python Object Store at time 26 = [ [2, 4, 6], [ ] ]

It is easier for us to read the "Values" stack backwards. The "Values" stack tells us that the list is in index 0 of the "Object Store" and we are looking to update index 1 in this list to 9.

Now, we know that:

the element at index 0 of stack (Python Object Store at time 26) = [2, 4, 6]

Thus, numbers is the list [2, 4, 6].

Then we can conclude:

Python Object Store at time (26 + 1) = result of storing (Python list (result of storing 9 at index 1 of stack [ 2, [ 4, [ 6, [ ] ] ] ])) at index 0 of stack (Python Object Store at time 26)

First, we know that:

result of storing 9 at index 1 of stack [ 2, [ 4, [ 6, [ ] ] ] ] = [ 2, [ 9, [ 6, [ ] ] ] ]

Recall that:

Python Object Store at time 26 = [ [2, 4, 6], [ ] ]

Now, since:

result of storing [2, 9, 6] at index 0 of stack [ [2, 4, 6], [ ] ] = [ [2, 9, 6], [ ] ]

We conclude that:

result of storing (Python list (result of storing 9 at index 1 of stack [ 2, [ 4, [ 6, [ ] ] ] ])) at index 0 of stack (Python Object Store at time 26) = [ [2, 9, 6], [ ] ]

Then:

Python Object Store at time (26 + 1) = [ [2, 9, 6], [ ] ]

Finally, since 26 + 1 = 27:

Python Object Store at time 27 = [ [2, 9, 6], [ ] ]

See the full proof here.

Try stepping through the simulator to see LW Python update the "Object Store."

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments