Get Item To Function Call Property
In LW Python, the "get item" operator is equal to the __getitem__ function call. More formally,
object[key]
= __getitem__(object, key)
LW Python uses this property to first convert the "get item" operation to a __getitem__ function call before evaluating the expression.
For example, suppose that:
statement at line 2, tab 0 = numbers[1]
We claim that:
numbers[1]
= __getitem__(numbers, 1)
Thus, we can conclude that:
statement at line 2, tab 0 = __getitem__(numbers, 1)
Try stepping through the simulator to see how LW Python retrieves the list element (item) at index 1:
Code Editor
LW Python State
Current Line | 1 | Current Tab | 0 | Time | 0 |
LW Python Simulator
Quiz (1 point)
Convert the following to a __getitem__ function call:
tokens[2]
Comments
Please log in to add comments