Hardware Reference
In-Depth Information
OOP is a handy way to group code and data together. Think about any object in the real world:
It has characteristics, also called attributes, and it has things that can be done to it. For example,
a balloon's attributes include its colour and if it is inflated. Actions that can be done to it include
blowing it up or popping it. You may have a room full of different balloons, but they can all be
considered to share the same set of attributes and actions (even though the value of the attri-
bute, like the colour, might be different). As such, they are all the same class of object.
In OOP, the programmer designs his or her own classes of objects, which have a set of attri-
butes and functions. In chicken.py , the Chicken class of the object is defined by class
Chicken(): . The next indented block defines the methods (another name for functions) of
the class. There are methods to start and stop the chicken from moving and one to make it
speak. he __init__ method is a special method that is called when an object is created. In
this case, it calls the pfio initialisation method (to initialise the PiFace interface) and sets up
the object's attributes. This can be handy, as you can use your object without worrying about
what you need to do to initialise things first. So far, the program has only created a class for
the chicken. It hasn't created an actual object, merely a description of what a Chicken object
will be like. It's a bit like creating a cookie cutter - you've defined what shape the cookies will
be, but you've not actually created any yet.
Creating Objects
A Chicken object is created in the test_chicken function. It is outside the class defini-
tion (and therefore not part of the Chicken class) because it's not within the indented block.
he statement tweetyPi = Chicken(pfio,0) creates a new Chicken object called
tweetyPi . The arguments are used to pass in the PiFace digital interface and identify which
pin the motor is connected to.
Breaking Up Your Code
Appendix B, “Introductory Software Project: The Insult Generator”, talks about splitting pro-
grams up into functions and how important it is to structure your code in computer science.
In this example, separate files are used for modules to help structure the program. Classes
also help by grouping related data and functions together.
As you have seen, what may have sounded like a daunting task of making a Twitter-enabled soft
toy move and talk becomes manageable when tackled in smaller chunks. You also tested each
chunk so it's easier to see where a problem is. Similarly, as you become more experienced,
you'll be able to take almost any big and seemingly hard problem, and split it up into little steps.
Why not try it? Think of a project you want to do, and then think how you can split it up into
smaller parts. Hopefully, as you complete the projects in this topic, you'll learn the skills neces-
sary to implement each of these little parts, and you'll be able to build almost anything!
 
Search WWH ::




Custom Search