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.

if value at method_name in map (value at "methods" in map (value at class_name in map class_defs)) = method_map, then definition of method method_name of class class_name in class_defs = method_map

Let's look at an example:

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

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 NameClass KeyValueMethod KeyValue
Personline1
bases[ ]
methods__init__paramsself, first_name, last_name, age
line2
age_in_monthsparamsself
line6

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:

  • The Class Definitions Map
  • The class definition
  • The methods of a class definition.
  • Each method

    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:

    KeyValue
    paramsself, first_name, last_name, age
    line2

    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.

    Quiz (1 point)

    On which line is the age_in_months method?

    Become a subscriber to save your progress, see the correct answer, and more!
    Quiz (1 point)

    What is the name of the age_in_months method parameter?

    Become a subscriber to save your progress, see the correct answer, and more!

    Comments

    Please log in to add comments