In the last lesson, I mislead you. Here’s the code we ended with:
class RepairedRobot(UsedRobot): def turn_right(self): for i in range(3): self.turn_left()
Open Reeborg’s Diary. If you want, you can also hide the world by clicking on the “World” button.
Enter and execute the following code and look at the result in the Diary:
r = UsedRobot() dir_js(r)
I use a single letter r for the robot name as this is a very short program and I don’t need a descriptive name.
We do not know if they are methods or attributes. __call__ starts and ends with two underscore characters; this is a convention in the Python world to denote some internal Python code that is mostly reserved for advanced programmers. The other is body. So, we know that r.body is something. Run the following code:
r = UsedRobot() dir_js(r.body)
Python programmers use a convention where variable names that start with an underscore, like _prev_x are meant to indicate that they are “private” and should normally not be changed by another programmer.
You should see something like:
x y objects orientation _is_leaky _prev_x _prev_y _prev_orientation
You might have guessed that objects refers to the various objects (tokens, etc.) carried by Reeborg. Also, various challenges told you that Reeborg is at the correct x position, and similarly for y. So it would seem likely that x and y refer to Reeborg’s position. As a programmer, your first reflex should be write a program and see if this is the case.
Select the world Empty, turn off the highlighting, and execute the following code:
jumper = UsedRobot() # mutant robot that teleports itself jumper.body.x = 8 jumper.body.y = 10
All you should see is a robot created at x=1, y=1 ... which might not be what you have expected. If you turn on the highlighting and run the program again, you will get the expected result.
Turn off the highlighting and add the following instruction:
at the end of your program and run it again.
You may recall from previous tutorials that Reeborg’s actions are recorded (like a movie) and played back one “frame” at a time. The recording of a given state happens when some special instructions are given. By changing the value of the attribute x or y of the jumper.body object, you do not trigger a frame recording. However, by adding a turn_left() instruction at the end, we do make a recording of the situation, which shows us that the previous instructions did indeed change the robot’s position. Alternatively, the code highlighing utility triggers a frame recording so that the line of code about to be executed can be shown with the more recent state of the world.
Don’t worry, you did not.
You already know about libraries; chances are there are some functions defined in yours on Reeborg’s World. Libraries are sometimes written in a different language than the main programming one. For numerical work, Fortran has long been the language of choice and most numerical libraries have been written in Fortran. Many other libraries have been written in the C language.
Python is sometimes described as a glue language. You can write Python programs that make use of functions found in Fortran and C libraries. Usually, to make use of such libraries, one refers to documentation written that indicates what functions can be called and how.