HomePython Page 2 - Object-Oriented Programming With Python (part 2)
The Family Tree - Python
With the basics out of the way, this concluding article discussesmore advanced aspects of Python's OO implementation, including inheritance,destructors and overrides.
One of the main virtues of object-oriented programming is that it allows you to re-use existing objects, and add new capabilities to them - a feature referred to as "inheritance". By creating a new object which inherits the properties and methods of an existing object, developers can build on existing Python classes, thereby reducing both development and testing time.
Python allows you to derive a new class from an existing class by specifying the name of the base class within parentheses while defining the new class. So, if I wanted to derive a new class named evenBiggerSnake() from the base class veryBigSnake(), my class definition would look something like this:
class evenBiggerSnake(veryBigSnake):
# method and property definitions
You can inherit from more than one base class as well.
class evenBiggerSnake(veryBigSnake, veryBigBird, veryBigFish):
# method and property definitions
A derived class functions in exactly the same manner as any
other class, with one minor change: in the event that a method or property accessed by an object is not found in the derived class, Python will automatically search the base class (and the base class's ancestors, if any exist) for that particular method or property.
As an example, let's create the new evenBiggerSnake() class, which inherits from the base class veryBigSnake().
class veryBigSnake:
# constructor
# now accepts name and type as arguments
def __init__(self, name="Peter Python", type="python"):
self.name = name
self.type = type
print "New snake in da house!"
# function to set snake name
def set_snake_name(self, name):
self.name = name
# function to set snake type
def set_snake_type(self, type):
self.type = type
# function to display name and type
def who_am_i(self):
print "My name is " + self.name + ", I'm a " + self.type + " and I'm
perfect for you! Take me home today!"
class evenBiggerSnake(veryBigSnake):
pass
At this point, you should be able to do this
>>> alpha = evenBiggerSnake()
New snake in da house!
>>> alpha.who_am_i()
My name is Peter Python, I'm a python and I'm perfect for you! Take me home
today!
>>> alpha.set_snake_name("Roger Rattler")
>>> alpha.set_snake_type("rattlesnake")
>>> alpha.who_am_i()
My name is Roger Rattler, I'm a rattlesnake and I'm perfect for you! Take
me home today!
>>>
and have the code work exactly as before, despite the fact
that you are now using the evenBiggerSnake() class. This indicates that the class evenBiggerSnake() has successfully inherited the properties and methods of the base class veryBigSnake().
This is sometimes referred to as the "empty sub-class test" - essentially, a new class which functions exactly like the parent class, and can be used as a replacement for it.
Note also that the derived class automatically inherits the base class's constructor if it doesn't have one of its own. However, if I did explicitly define a constructor for the derived class, this new constructor would override the base class's constructor.
class evenBiggerSnake(veryBigSnake):
# constructor
# accepts name, age and type as arguments
def __init__(self, name="Paul Python", type="python", age="2"):
self.name = name
self.age = age
self.type = type
print "A new, improved snake has just been born"
Look what happens when I create an instance of the class now:
>>> alpha = evenBiggerSnake()
A new, improved snake has just been born
>>> alpha.name
'Paul Python'
>>> alpha.age
'2'
>>> alpha.who_am_i()
My name is Paul Python, I'm a python and I'm perfect for you! Take me home
today!
>>>
This is true of other methods too - look what happens when I
define a new who_am_i() method for the evenBiggerSnake() class:
class evenBiggerSnake(veryBigSnake):
# constructor
# accepts name, age and type as arguments
def __init__(self, name="Paul Python", type="python", age="2"):
self.name = name
self.age = age
self.type = type
print "A new, improved snake has just been born"
# modified function to display name, age and type
def who_am_i(self):
print "My name is " + self.name + ", I'm a " + self.type +
" and I'm just " + self.age + " years old"
Here's the output:
>>> alpha = evenBiggerSnake()
A new, improved snake has just been born
>>> alpha.who_am_i()
My name is Paul Python, I'm a python and I'm just 2 years old
>>>