Breeds of dogs with constructors
Code listing
class Dog:
def __init__(self):
self.height = 4
def bark(self):
print "woof"
def wag(self):
print "wag wag wag"
class Dachshund(Dog):
def __init__(self):
self.height = 1
def dig(self):
print "dig dig dig"
def bark(self):
print "arf arf"
class Collie(Dog):
def rescue(self, person):
print "Come quickly, " + person + " fell down the mineshaft"
Command area transcript
>>> lassie = Collie()
>>> lassie.height
4
>>> pickles = Dachshund()
>>> pickles.height
1
>>> lassie.bark()
woof
>>> pickles.bark()
arf arf
>>> pickles.dig()
dig dig dig
>>> lassie.dig()
The error was:dig
Attribute not found.
You are trying to access a part of the object that doesn't exist.
AttributeError: dig
>>>
Link to this Page