Tuesday, 28 September 2010

Object Orientation Example

I knocked this example up today for a friend on IRC. Thought I would post it here.
#! /usr/bin/env python
################################################################################
##  Python Object Orientated example by rowinggolfer                          ##
##  the same object... but extra functionality added in layers                ##
##                                                                            ##
##  A noteable "Gotcha" for newbs is the __init__ function.                   ##
##  the base class in this example sets some important attributes in this     ##
##  function. Therefore classes which inherit from it, but be careful if      ##
##  re-implementing this function                                             ##
##  NOTE - the teenager class does NOT overwrite this function... hence when  ##
##  a teenager is initiated, python looks up the class hierarchy and executes ##
##  the first __init__ method it finds.. in this case the Toddler's __init__  ##
##                                                                            ##
##                                                                            ##
##  Run this script, and study the output.                                    ##
##                                                                            ##
##  Pay special attention to the attribute "description, and see how it is    ##
##  altered mid method in the teenage years.                                  ##
##                                                                            ##
################################################################################

from datetime import date
from base64 import b64decode

class TimritBaby(object):
    '''
    a baby has a date of birth, a name, and limited functionality
    '''
    def __init__(self, date_of_birth):
        print "TimritBaby initiated"
        self.name = 'Scott Murtz'
        self.dob = date_of_birth
        self.description = "(a baby)"

    def poop(self):
        print "    %s %s can Poop!"% (self.name, self.description)


class TimritToddler(TimritBaby):
    '''
    a toddler has a new method
    '''
    def __init__(self, dob):
        # call the init method of the base class..
        # otherwise the name attribute won't work!!
        print "\nTimritToddler initiated"
        TimritBaby.__init__(self, dob)
        self.description = "(a toddler)"
    
    def walk(self):
        print "    %s %s can Walk!"% (self.name, self.description)

class TimritTeenager(TimritToddler):
    '''
    a teenager with a secret habit
    '''
    def private_time(self):
        self.description = "(now a teeneager)"
        message = b64decode('aXMgbWFzdHVyYmF0aW5nIGFnYWlu')
        print "    %s %s %s"% (self.name, self.description, message)


if __name__ == "__main__":
    baby = TimritBaby(date(1968,1,1))
    baby.poop()
    try:
        baby.walk()
    except AttributeError as e:
        print "ERROR - WHOOPS!", e
    
    teenager = TimritTeenager(date(1968,1,1))
    teenager.poop()
    teenager.walk()
    teenager.private_time()
    teenager.walk()
 

No comments: