Remember the rule about avoiding repetitions:
What we are going to see in this chapter is a way to avoid "reinventing the wheel".
Remember when we introduced the turn_right() instruction:
def turn_right(): turn_left() turn_left() turn_left()
We did that to avoid having to type turn_left() three times in a row every time we wanted to instruct Reeborg to turn right. However, we still have a lot of repetition since almost every time we write a new program, we end up defining turn_right() again. There is, however, a way to avoid this. Create the following program and try to run it.
from useful import turn_right turn_right() turn_off()
I have created for you a module named "useful" containing some newly defined instructions. Rather than explaining what each new instruction does, here's a sample program with the corresponding screen capture.
from useful import * turn_left() move() set_trace_style(1, 'red') turn_right() set_trace_style(2, 'blue') move() set_trace_style(1, 'green') climb_up_east() set_trace_style(2, 'blue') move() set_trace_style(1, 'orange') turn_around() set_trace_style(2, 'sea green') climb_up_west() set_trace_style(1, 'red') turn_around() set_trace_style(0, 'blue') # 0 = invisible repeat(move, 4) set_trace_style(1, 'green') climb_down_east() set_trace_style(2, 'blue') move() set_trace_style(1, 'black') turn_around() set_trace_style(2, 'red') climb_down_west() turn_off()
Rather than importing every new instruction one by one, as we did in the first example with turn_right(), we have use the "*" notation to indicate "all definitions". Before going any further, I should probably make a few comments:
Suppose you want to use the name "turn_right" to mean something other than what is defined within useful. There are a few ways to do this.
# The following is useful for French speakers: from useful import turn_right as vire_a_droite # Use it: vire_a_droite() turn_off()
If you consider the potential usefulness of the module "useful" in writing your own programs ... you might think that it is too limited to be useful after all. You can reflect that by writing the following:
# Importing the module import useful as not_so_useful_after_all # Use the various instructions: not_so_useful_after_all.turn_right() not_so_useful_after_all.turn_around() turn_off()In real life, the normal use of this type of import statement is more along the following lines:
# Importing the module import module_with_very_very_very_long_name as short_name # Use the various instructions: short_name.instruction1() short_name.instruction2()
It is often said that Python comes with batteries included. That is to say, Python includes many useful modules written by very smart people. These modules have been thoroughly tested to be free of bugs and written very efficiently. When you learn more about Python and you want to write your own "complicated" programs, it will be really worthwhile to first determine if any relevant module already exists and which could help you in your task. I will introduce you to a few useful modules in later lessons.
Important note to teachers: as alluded to above, the reason to restrict the usage of import statements is to avoid someone getting hold of an unsafe script and passing it along to an unsuspecting victim. Note that this can still be done in the Python editor window but, hopefully, by then students will know enough to check programs given by "friends" before trying them out. That being said, it is fairly easy to introduce a small change in rur-ple to allow arbitrary import statements. This can be useful in a classroom setting. Please contact the author if you have a need to do this, and cannot figure it out on your own.