Python
  Home arrow Python arrow Page 2 - Object-Oriented Programming With Pytho...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Object-Oriented Programming With Python (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2001-11-26

    Table of Contents:
  • Object-Oriented Programming With Python (part 2)
  • The Family Tree
  • Alarm Bells
  • Under The Microscope
  • Chaos And Destruction

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Object-Oriented Programming With Python (part 2) - The Family Tree


    (Page 2 of 5 )

    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 >>>

    More Python Articles
    More By icarus, (c) Melonfire


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway