Find Method Definition Property
LW Python searches the Class Definitions Map to get information about a class method (such as the line number and the method parameters). This property shows us how to get the method definition.
Let's look at an example:
Current Line | 1 | Current Tab | 0 | Time | 0 |
After LW Python has finished processing the class definition, the following is true:
value at "__init__" in map (value at "methods" in map (value at "Person" in map [ entry "Person": [ entry "bases": [ ], [ entry "methods": [ entry "__init__": [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ], [ entry "age_in_months": [ entry "params": [ self, [ ] ], [ entry "line": 6, [ ] ] ], [ ] ] ], [ entry "line": 1, [ ] ] ] ], [ ] ])) = [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ]
This is pretty hard to read, so let's break it down a bit. There is a Class Definitions Map as follows:
[ entry "Person": [ entry "bases": [ ], [ entry "methods": [ entry "__init__": [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ], [ entry "age_in_months": [ entry "params": [ self, [ ] ], [ entry "line": 6, [ ] ] ], [ ] ] ], [ entry "line": 1, [ ] ] ] ], [ ] ]
or in table form:
Class Name | Class Key | Value | Method Key | Value |
---|---|---|---|---|
Person | line | 1 | ||
bases | [ ] | |||
methods | __init__ | params | self, first_name, last_name, age | |
line | 2 | |||
age_in_months | params | self | ||
line | 6 |
This map has 1 entry for the "Person" class. The "Person" class has 2 methods, "__init__" and "age_in_months."
There are many levels of maps here:
All of the above are maps, and that is why we have to look up many maps to get to information about a method.
First, we get the "Person" class definition. Then we get the "methods" of this class. Finally, we get the "__init__" method from the "methods" map.
The result is: [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ]
Now, given the conditions above, this property says that:
definition of method "__init__" of class "Person" in [ entry "Person": [ entry "bases": [ ], [ entry "methods": [ entry "__init__": [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ], [ entry "age_in_months": [ entry "params": [ self, [ ] ], [ entry "line": 6, [ ] ] ], [ ] ] ], [ entry "line": 1, [ ] ] ] ], [ ] ] = [ entry "params": [ self, [ first_name, [ last_name, [ age, [ ] ] ] ] ], [ entry "line": 2, [ ] ] ]
Here is the "__init__" method definition in table form:
Key | Value |
---|---|
params | self, first_name, last_name, age |
line | 2 |
The conclusion is just a slightly more convenient way to say the condition ("value for "__init__" from map (value for "methods" from map (value for "person ... = method definition map).
Humans can simply read the code to find the methods and their parameters, but LW Python reads the Class Definitions Map to get the same inforamation.
On which line is the age_in_months method?
What is the name of the age_in_months method parameter?
Comments
Please log in to add comments