Hardware Reference
In-Depth Information
Imagine if you had multiple chickens, one connected to each of the relays on PiFace. You
could create two chickens by typing the following:
tweetyPi1 = Chicken(pfio,0)
tweetyPi2 = Chicken(pfio,1)
Now, if you wanted to start them both wobbling you could type this:
tweetyPi1.start_wobble()
tweetyPi2.start_wobble()
By using objects, you can use the same function for each chicken. You don't have to worry
about which pin they are connected to after you've created the object because the object
stores it for you. This is an example of why using OOP can be advantageous.
OOP can be tricky to understand at first, but it should become clearer as you see more exam-
ples. For now, you can ignore the details, accept that it works and hide it in the chicken mod-
ule, in the same way you use other modules without knowing what is inside them. Believe it
or not, professional programmers do this too - as long as the interface to a module is clear
(that is, the functions it provides are clearly documented), it doesn't matter if they don't fully
understand how it works!
Testing As You Go
If you wrote a huge program and tried to run it, chances are it wouldn't work the first time.
You'd then have to go through all of it trying to find where the problem was. Instead, it is
better to test as you go, checking each component before moving on to the next. Python
provides a good means to do this. Toward the end of the file are the following lines:
if __name__ == '__main__':
test_chicken()
This code calls the test_chicken() function if the file is being run by itself, but doesn't
call the function if it is imported as a module. As such, it's a good way of writing code that
will test the behaviour of a module. As you learn more about programming, you will under-
stand the importance of testing and which tools and techniques can help.
Surrounding or starting a word with __ in Python (and some other languages) indicates a
special meaning. As such, it's better not to start and end your own variables and functions this
way unless you really know what you're doing!
TIP
 
Search WWH ::




Custom Search