List Get Item Property

This property describes how Python retrieves an element from a list in the "Object Store."

if the following are true:
  • Expression Stack at time t = [ [ ], [ [ function call with name: "__getitem__" and arguments: args, ys ], rest ] ]
  • Value Stack at time t = [ [ a, [ Python reference idx, [ ] ] ], [ next_level, other_levels ] ]
  • the element at index idx of stack (Python Object Store at time t) = Python list entries

then Value Stack at time (t + 1) = [ [ the element at index a of stack entries, next_level ], other_levels ]

For example, suppose that the following are true:
  • Expression Stack at time 11 = [ [ ], [ [ __getitem__(numbers, 1), [ ] ], [ ] ] ]
  • Value Stack at time 11 = [ [ 1, [ Python reference 0, [ ] ] ], [ [ ], [ ] ] ]
  • Python Object Store at time 11 = [ [2, 4, 6], [ ] ]

First, we know that:

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

Then we can conclude that:

Value Stack at time (11 + 1) = [ [ the element at index 1 of stack [ 2, [ 4, [ 6, [ ] ] ] ], [ ] ], [ ] ]

We also know that:

the element at index 1 of stack [ 2, [ 4, [ 6, [ ] ] ] ] = 4

Thus, we can conclude that:

Value Stack at time (11 + 1) = [ [ 4, [ ] ], [ ] ]

Which simplifies to:

Value Stack at time 12 = [ [ 4, [ ] ], [ ] ]

The value stack tells LW Python that the "numbers" list is stored in index 0. And we can see that there is a list [2, 4, 6] in index 0 of the "Object Store." Then at time 12, the element at index 1 of the list [2, 4, 6], which is 4, is pushed to the top of the Values Stack.

See the full proof here.

Try stepping through the simulator to see LW Python retrieve the element from the list in the "Object Store."

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments