Get Dictionary Item Property

After LW Python evaluates the dictionary name to some reference and the key, LW Python looks up the dictionary object in the Object Store and retrieves the value for the evaluated key. This value is pushed to the top of the Values Stack.

More formally:

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 dictionary entries

then Value Stack at time (t + 1) = [ [ value at a in map entries, next_level ], other_levels ]

The Values Stack does not contain the dictionary object. Instead, it contains a reference, or location. LW Python uses this location to retrieve the dictionary from the Object Store. Then LW Python will search the dictionary for an entry with the specified key and retrieve the entry value.

For example, suppose that the following are true:

  • Expression Stack at time 16 = [ [ ], [ [ __getitem__(locations, "ES"), [ ] ], [ ] ] ]
  • Value Stack at time 16 = [ [ "ES", [ Python reference 0, [ ] ] ], [ [ ], [ ] ] ]
  • Python Object Store at time 16 = [ {"ES": "Spain", "MX": "Mexico"}, [ ] ]

We claim that:

the element at index 0 of stack (Python Object Store at time 16) = {"ES": "Spain", "MX": "Mexico"}

Then using the Get Dictionary Item Property, we conclude that:

Value Stack at time (16 + 1) = [ [ value at "ES" in map [ entry "ES": "Spain", [ entry "MX": "Mexico", [ ] ] ], [ ] ], [ ] ]

We claim that:

value at "ES" in map [ entry "ES": "Spain", [ entry "MX": "Mexico", [ ] ] ] = "Spain"

Thus we conclude that:

Value Stack at time (16 + 1) = [ [ "Spain", [ ] ], [ ] ]

Which simplifies to:

Value Stack at time 17 = [ [ "Spain", [ ] ], [ ] ]

See here for the full proof.

Try stepping through the simulator to see LW Python push the entry value for the key "ES" to the Values Stack.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments