Found Method Map in Base Class Property

When LW Python is searching for a method definition in a list of base classes, it starts with the first base class and stops as soon as it finds the method. This property helps LW Python find the correct method definition from a list of base classes.

if definition of method method_name of class first_base in class_defs = method_map, then definition of method method_name of base classes [ first_base, rest ] in class_defs = method_map

If two base classes happen to have the same method, the class that comes later is not considered. Let's see an example of this property. Consider the following program:

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

The "Class Definitions" map is as follows:

[ entry "Animal": [ entry "bases": [ ], [ entry "methods": [ entry "__init__": [ entry "params": [ self, [ ] ], [ entry "line": 2, [ ] ] ], [ entry "move": [ entry "params": [ self, [ ] ], [ entry "line": 5, [ ] ] ], [ ] ] ], [ entry "line": 1, [ ] ] ] ], [ entry "Dog": [ entry "bases": [ "Animal", [ ] ], [ entry "methods": [ entry "jump": [ entry "params": [ self, [ ] ], [ entry "line": 8, [ ] ] ], [ ] ], [ entry "line": 7, [ ] ] ] ], [ ] ] ]

Here is a table view:

Class NameClass KeyValueMethod KeyValue
Animalline1
bases[ ]
methods__init__paramsself
line2
moveparamsself
line5
Dogline7
basesAnimal
methodsjumpparamsself
line8

There are 2 entries, 1 for the "Animal" class and another for the "Dog" class.

For conciseness, let's say that the above map is equal to the variable "class_defs."

Now, we claim that:

definition of method "__init__" of class "Animal" in class_defs = [ entry "params": [ self, [ ] ], [ entry "line": 2, [ ] ] ]

then we can conclude:

definition of method "__init__" of base classes [ "Animal", [ ] ] in class_defs = [ entry "params": [ self, [ ] ], [ entry "line": 2, [ ] ] ]

The conclusion is very similar to the condition, except that there is a list ["Animal", []] instead of just "Animal" in the condition. So when is this property useful? Notice that ["Animal", []] is the base class name list for the "Dog" class. Thus, we can use this property to show that the "Dog" class's "__init__" method is equal to the "Animal" class's "__init__" method.


Comments

Please log in to add comments