Method Definition Property
When LW Python reads a new method definition statement, it updates the "Class Definitions" map with the method name, the method parameters, and the current line number. More formally,
if the following are true:
- the line at time t = i
- the tab at time t = j
- statement at line i, tab j = a method named method_name with parameters params
- value at class_name in map (Class Map at time t) = class_map
- value at "methods" in map class_map = methods_map
then Class Map at time (t + 1) = result of storing (result of storing (result of storing [ entry "params": params, [ entry "line": i, [ ] ] ] at key: method_name in map: methods_map) at key: "methods" in map: class_map) at key: class_name in map: (Class Map at time t)
Let's break down the conclusion. The method definision is inside the "methods" map, which is inside a "class definition" map, which is finally inside the "Class Definitions" map. Then, to update a method definition, LW Python updates all 3 maps. For example,
if the following are true:
- the line at time 1 = 2
- the tab at time 1 = 1
- statement at line 2, tab 1 =
def __init__(self, first_name, last_name, age):
- value at "Person" in map (Class Map at time 1) = [ entry "bases": [ ], [ entry "methods": [ ], [ entry "line": 1, [ ] ] ] ]
- value at "methods" in map [ entry "bases": [ ], [ entry "methods": [ ], [ entry "line": 1, [ ] ] ] ] = [ ]
then Class Map at time (1 + 1) = result of storing (result of storing (result of storing [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ] at key: "__init__" in map: [ ]) at key: "methods" in map: [ entry "bases": [ ], [ entry "methods": [ ], [ entry "line": 1, [ ] ] ] ]) at key: "Person" in map: (Class Map at time 1)
That is pretty hard to read, so let's try a table view.
Before the method data is added, the "Person" class definition is:
Key | Value | |
---|---|---|
line | 1 | |
bases | [ ] | |
methods | [ ] |
Key | Value |
---|---|
params | [self, [first_name, [last_name, [age, []]]]] |
line | 2 |
Thus the new "Person" class definition is:
Key | Value | |
---|---|---|
line | 1 | |
bases | [ ] | |
methods | __init__ : "__init__" method definition |
Finally, the "Person" entry in the "Class Definitions" map is updated with the new class definition map above. In words, the conclusion is:
Class Map at time 2 = [ entry "Person": [ entry "bases": [ ], [ entry "methods": [ entry "__init__": [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ], [ ] ], [ entry "line": 1, [ ] ] ] ], [ ] ]
Try stepping through the simulator to see LW Python save the method definition.
Current Line | 1 | Current Tab | 0 | Time | 0 |
Comments
Please log in to add comments