Get List Length Property

This property describes how LW Python computes the length of a list. LW Python first looks in the "Value" stack to find the location of the list in the "Object Store," then counts the number of elements in the list. More formally,

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

then Return Value at time (t + 1) = length of stack entries

For example, suppose that:
  • expression state at time 20 = "call_function_begin"
  • Value Stack at time 20 = [ [ Python reference 0, [ ] ], [ ] ]
  • the expression at time 20 = len(numbers)
  • Python Object Store at time 20 = [ [2, 4, 6], [ ] ]
The "Values" stack tells us that the list is in index 0 of the "Object Store."

We know that:

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

Thus, we conclude that:

Return Value at time (20 + 1) = length of stack [ 2, [ 4, [ 6, [ ] ] ] ]

Now, we know that:

length of stack [ 2, [ 4, [ 6, [ ] ] ] ] = 3

Thus,

Return Value at time (20 + 1) = 3

Since 20 + 1 = 3, we conclude that:

Return Value at time 21 = 3

See the full proof here.

Try stepping through the simulator to see how LW Python computes the length of a list.

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments