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,
- Value Stack at time t = [ [ Python reference idx, [ ] ], [ next_level, other_levels ] ]
- Expression Stack at time t = [ [ ], [ [ function call with name: "len" and arguments: args, ys ], rest ] ]
- the element at index idx of stack (Python Object Store at time t) = Python list entries
then Value Stack at time (t + 1) = [ [ length of stack entries, next_level ], other_levels ]
- Expression Stack at time 10 = [ [ ], [ [
len(numbers)
, [ ] ], [ ] ] ] - Value Stack at time 10 = [ [ Python reference 0, [ ] ], [ [ ], [ ] ] ]
- Python Object Store at time 20 = [
[2, 4, 6]
, [ ] ]
We know that:
the element at index 0 of stack (Python Object Store at time 10) = [2, 4, 6]
Thus, we conclude that:
Value Stack at time (10 + 1) = [ [ length of stack [ 2, [ 4, [ 6, [ ] ] ] ], [ ] ], [ ] ]
Now, we know that:
length of stack [ 2, [ 4, [ 6, [ ] ] ] ] = 3
Thus,
Value Stack at time (10 + 1) = [ [ 3, [ ] ], [ ] ]
Which simplifies to:
Value Stack at time 11 = [ [ 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