Title: Python Programming
1Python Programming
2Building a Base Class
class Animal define the base class "Animal
the constructor def __init__(self,type,weight)
self.__type type
self.__weight weight
3Adding the get Methods
get method for __type variable def
get__type(self) return self.__type
get method for __weight variable def
get__weight(self) return self.__weight
4Adding the set Methods
set method for __type variable def
set__type(self,type) self.__type
type set method for __weight variable def
set__weight(self,weight) self.__weight
weight
5Adding a String Representation
the string representation of an Animal object
def __str__(self) return "Type"
self.__type " " "Weight" self.__weight
6Adding a Speak Method
animal objects can say "Hello" def
speak(self) print "I can only say
'Hello'"
7Testing the Class
print "Testing Animal methods" animal1 Animal(
"frog", "2 ounces") constructor returns new
animal object print animal1.get__type()
call Animal method get__type() print
animal1.get__weight() call Animal
method get__weight() animal1.set__type("horse")
test the set__type() method animal1.set_
_weight("400 pounds") test the set__weight()
method print animal1.get__type() print
animal1.get__weight() animal1.speak()
test the speak() method print animal1
test the __str()
method! print "Finished testing Animal methods"