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
- 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]
, [ ] ]
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.
Current Line | 1 | Current Tab | 0 | Time | 0 |
Comments
Please log in to add comments