Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

  1. Exit the nano text editor by pressing Ctrl+X.

  2. Before making the improvements, make sure all is well with your code by typing python3
    py3prog/script1503.py. If you get any errors, double-check the four files for any
    typos and make corrections as needed. Next you will begin the improvements.

  3. Create a base class called Sighting and subclass called BirdSighting. For
    simplicity’s sake, you can put them both in one module file. Type nano
    py3prog/sightings.py and press Enter. Python puts you into the nano text editor and
    creates the object module file py3prog/sightings.py.

  4. Type the following code into the nano editor window:


Click here to view code image


# Sightings base class
#
class Sighting:
#Initialize Sighting class data attributes
def __init__(self, sight_location, sight_date):
self.__sight_location = sight_location #Location of sighting
self.__sight_date = sight_date #Date of sighting

#Mutator methods for Sighting data attributes
def set_sight_location(self, sight_location):
self.__sight_location = sight_location

def set_sight_date(self, sight_date):
self.__sight_date = sight_date
#Accessor methods for Sighting data attributes
def get_sight_location(self):
return self.__sight_location
def get_sight_date(self):
return self.__sight_date
#
# Bird Sighting subclass (base class: Sighting)
#
class BirdSighting(Sighting):
#Initialize Bird Sighting data attributes & obtain Bird inheritance.
def __init__(self, sight_location, sight_date,
bird_species, flock_size):
#Obtain base class data attributes & methods (inheritance)
Sighting.__init__(self, sight_location, sight_date)

#Initialize subclass data attributes
self.__bird_species = bird_species #Bird type
self.__flock_size = flock_size #How many in flock.

#Mutator methods for Bird Sighting data attributes
def set_bird_species(self,bird_species):
self.__bird_species = bird_species
def set_flock_size(self,flock_size):
self.__flock_size = flock_size
Free download pdf