Page 57 - Programming the Raspberry Pi Getting Started with Python
P. 57

5


                                                    Modules, Classes, and Methods

          In this chapter, we discuss how to make and use our own modules, like the random module we used in
          Chapter 3. We also discuss how Python implements object orientation, which allows programs to be
          structured  into  classes,  each  responsible  for  its  own  behavior. This  helps  to  keep  a  check  on  the
          complexity of our programs and generally makes them easier to manage. The main mechanisms for
          doing  this  are  classes  and  methods. You  have  already  used  built-in  classes  and  methods  in  earlier
          chapters without necessarily knowing it.
          Modules
          Most computer languages have a concept like modules that allows you to create a group of functions
          that are in a convenient form for others to use—or even for yourself to use on different projects.

             Python does this grouping of functions in a very simple and elegant way. Essentially, any file with
          Python code in it can be thought of as a module with the same name as the file. However, before we
          get into writing our own modules, let’s look at how we use the modules already installed with Python.
          Using Modules
          When we used the random module previously, we did something like this:





             The first thing we do here is tell Python that we want to use the random module by using the import

          command. Somewhere in the Python installation is a file called random.py that contains a randint
          function as well as some other functions.
             With so many modules available to us, there is a real danger that different modules might have
          functions with the same name. In such a case, how would Python know which one to use? Fortunately,
          we do not have to worry about this happening because we have imported the module, and none of the

          functions in the module are visible unless we prepend the module name and then a dot onto the front
          of the function name. Try omitting the module name, like this:









             Having to put the module name in front of every call to a function that’s used a lot can get tedious.
          Fortunately, we can make this a little easier by adding to the import command as follows:






             This gives the module a local name within our program of just r rather than random, which saves us
          a bit of typing.
             If you are certain a function you want to use from a library is not going to conflict with anything in
          your program, you can take things a stage further, as follows:





             To go even further, you can import everything from the module in one fell swoop. Unless you know
          exactly what is in the module, however, this is not normally a good idea, but you can do it. Here’s
          how:
   52   53   54   55   56   57   58   59   60   61   62