Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Breeds of dogs

# Make sure you understand class syntax
class Dog:

  # Bark is a method that dogs have / can do. Note the indentation.
  # Self is the placeholder argument referring to "this" object
  # (e.g. lassie or bibi, or whoever the dog is.)
  def bark(self):
    print "woof"

  def wag(self):
    print "wag wag wag"

# Note that Dachshund is a subclass of Dog.
# Dog, however, wasn't a subclass of anything more general.
class Dachshund(Dog):
  def dig(self):
    print "dig dig dig"

  def bark(self):
    print "arf arf"

# Like Dachshunds, Collies are dogs
class Collie(Dog):

  # rescue is a method that has two arguments:
  # The object itself and a person name.
  def rescue(self, person):
    print "Come quickly, " + person + " fell down the mineshaft"

# The test program. Contains a deliberate error and will crash on final line.
def dogShow():
  # We create objects by using the constructor, which is a
  # "function" with the same name as the class and no arguments.
  # Note the convention that class and constructor names ALWAYS
  # start with a capital letter, whereas objects (instances) don't.
  bibi = Dachshund()

  # Note that when called, the value of the self parameter (bibi or lassie)
  # moves from inside the parentheses and precedes the dot.
  bibi.bark() # Should sound like "arf arf"
  lassie = Collie()
  lassie.bark() # Should sound like "woof"
  bibi.dig() # Works ok.
  lassie.rescue("Timmy") # Should do a Lassie-type thing
  lassie.dig() # Should fail with an error. Dig is not something Collie's can do.


Link to this Page