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 Stack at time t = [ [ ], [ [ function call with name: "__setitem__" and arguments: args, ys ], rest ] ]
  • Value Stack at time t = [ [ value, [ key, [ Python reference idx, [ ] ] ] ], [ next_level, other_levels ] ]
  • 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 Stack at time 12 = [ [ ], [ [ __setitem__(numbers, 1, 9), [ ] ], [ ] ] ]
  • Value Stack at time 12 = [ [ 9, [ 1, [ Python reference 0, [ ] ] ] ], [ [ ], [ ] ] ]
  • Python Object Store at time 12 = [ [2, 4, 6], [ ] ]

Then we can conclude that:

Python Object Store at time (12 + 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 12)

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.

Note that:

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

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

We claim that:

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

Recall that:

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

We also claim that:

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

Then after substituting 2 terms, 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 12) = [ [2, 9, 6], [ ] ]

Then:

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

Finally, since 12 + 1 = 13:

Python Object Store at time 13 = [ [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