Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Math Modules
Code Introspection

Objective

Inspect some old items tucked away in the attic by using code introspection to find out what they are and how they’re used.

The attic has an office that’s rarely used and there are a few old chests containing some valuables that have been long forgotten. Unless otherwise stated or unless a memo provides the info, sometimes it can be rather difficult to identify items, or constants being collected on the field. Not only that, it can be hard to identify the range of things you can accomplish with a specific object or what it’s properties are. In order to address that we can use Code Introspection.

Code Introspection allows us to inspect and identify certain snippets of code and indicate some of it’s properties. For this we will be using four (4) built in introspection functions packaged with the base python code: type() , id() , repr() , dir() and help() . They function as follows:

  • type() : Function informs you what type of object the argument you place is. For example, if you place a variable as an argument, the function will identify the object as a variable.
  • id(): Every object ever created in python has a unique id that identifies the object within code. By placing an object as an argument of this function it will return the objects unique id.
  • repr(): Converts a non string value into a string, useful for converting data into a readable form.
  • dir(): Displays all attributes the function posses, useful for knowing what can be achieved by a specific object type.
  • help(): Displays all information about a given object, function, class, etc. that’s available in python’s registry.

In addition we have a new async function being the display() function. This function is similar to the read() function but allows you to display dynamic data, much like the code introspection functions which produce dynamic data. It’s syntax is as follows: await player.dysplay( -data-) . All of these functions will be useful for analyzing data, this is useful for when we use Methods and begin importing new functions as well as for analyzing any code we want to identify properties from.

We will be using these functions in order to identify and catalogue the items stored in the attic. To start off, walk to the dark X marks over the colored carpets starting with the one over the red carpet. There are three (3) constants in this area: red_container , green_container , and blue_container , these constants represent containers in each chest corresponding to each colored carpet.

For each X mark, face the chest and use the open() function to open the chests, use the collect() function with the constant of the corresponding colored carpet to grab it from the chest. Use the type() introspection function to identify what type of constant is in the chest, the output it produces is raw data and for this we use the repr() function to convert it into a string. Use the speak() to display the resulting string from combining the functions and the constant, for example: player.speak(repr(type(red_container))) . Follow this up with the speak() function with the constant to identify the contents directly, for example: player.speak(red_container) .

Once all three (3) of the constants have been collected, introspected and identified, walk to the light X mark in front of the desk. Create three variables named: item_id_red , item_id_green , item_id_blue , these will be used to store id numbers for each of the constants you collected. Use the id() introspect function to get the constant’s unique identification number by adding it as an argument. Match the name of the variable with the name of the constant using this format as an example: item_id_red = id(red_container) , do this for all three (3) variables and constants.

Once all three(3) variables have been populated, store them all in a list named item_list and on the light X mark use the display() function to make sure it's noted on the memo on the table, like this: await player.display(item_list) .

Now that the list was been charted down, walk to the dark X mark over the purple carpet and face the desk. Here we’re going to use the dir() function do identify what attributes the list has, however the output is raw data which can be tricky to display. For this we use the repr() function to transform the data into a readable string. Create a variable named item_attributes and store the resulting string, like this: item_attributes = repr(dir(item_list)). Finally use the display() function to ensure it's noted on the memo on the table.

Walk to the dark X mark over the white carpet to do the same as you did with dir() but with help() , this will outline all documentation usage on the object. Create a variable named item_help and store the resulting string, like this: item_help = repr(help(item_list)). Finally use the read() function to ensure it's noted on the memo on the table as well in order to complete the level.

Code book