Python
  Home arrow Python arrow Page 4 - Object Orientation in Python
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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 Orientation in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    2005-02-14

    Table of Contents:
  • Object Orientation in Python
  • Another Way to Understand Object Orientation
  • Classes in Python
  • More About Creating Classes

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Object Orientation in Python - More About Creating Classes
    (Page 4 of 4 )

     

    I mentioned subclasses and inheritance in the previous section. We will now take a look at how to create a class that derives its methods and variables from another class:

     

    class ParentClass:

      testVariableOne = 'This is a test. I promise.'

      testVariableTwo = 'No, really, it is just a test.'

      def __init__ ( self, testArgument ):

         self.testVariableThree = testArgument

      def testMethod ( self ):

         pass

     

    class ChildClass ( ParentClass ):

      pass

     

    parentObject = ParentClass ( 'Hello, how are you?' )

    print parentObject.testVariableOne # "This is a test. I promise."

    print parentObject.testVariableTwo # "No, really, it is just a test."

    print parentObject.testVariableThree # "Hello, how are you?"

     

    childObject = ChildClass ( 'I am fine, thank you.' )

    print childObject.testVariableOne # "This is a test. I promise."

    print childObject.testVariableTwo # "No, really, it is just a test."

    print childObject.testVariableThree # "I am fine, thank you."

     

    Subclasses are perfectly capable of defining their own variables and methods, too. They can also overwrite variables and methods passed down from the original class:

     

    class ParentClass:

      testVariableA = 'This is a test variable. Pay no attention to it.'

      testVariableB = 'This is also a test variable. Or is it? We may never know.'

      def __init__ ( self, testArgument ):

         self.testVariableC = testArgument

      def testMethod ( self ):

         return False

     

    class ChildClass ( ParentClass ):

      testVariableB = 'I am a very rebellious variable. Yes I am.'

      def testMethod ( self ):

         return True

     

    parentObject = ParentClass ( 'I am an insignificant test argument.' )

    print parentObject.testVariableA # "This is a test variable. Pay no attention to it."

    print parentObject.testVariableB # "This is a test variable. Or is it? We may never know."

    print parentObject.testVariableC # "I am an insignificant test argument"

    print parentObject.testMethod() # False

     

    childObject = ChildClass ( 'This is a subclass of ParentClass.' )

    print childObject.testVariableA # "This is a test variable. Pay no attention to it."

    print childObject.testVariableB # "I am a very rebellious variable. Yes I am."

    print childObject.testVariableC # "This is a subclass of Parent Class."

    print childObject.testMethod() # True

     

    If you only want to add to a parent's method, this is also possible by calling the class's method and supplying the needed arguments:

     

    class ParentClass:

      def testMethod ( self, testArgument ):

         return str ( len ( testArgument ) )

    class ChildClass:

      def testMethod ( self, testArgument ):

         return ParentClass.testMethod ( self, testArgument ) + ' ( Extra Stuff )'

     

    parentObject = ParentClass()

    print parentObject.testMethod ( 'This is a long string.' ) # "22"

     

    childObject =ChildClass()

    print childObject.testMethod ( 'This is a long string.' ) # "22 ( Extra Stuff )"

     

    It is also possible to create a subclass with multiple parents:

     

    class ParentClassOne:

      testVariableA = "This is another test. In fact, this is the last one."

     

    class ParentClassTwo:

      testVariableB = "Caffeine fuels the world."

      def testMethodA ( self ):

         return True

     

    class ParentClassThree:

      def testMethodB ( self ):

         return False

     

    class ChildClass ( ParentClassOne, ParentClassTwo, ParentClassThree ):

      pass

     

    anObject = ChildClass()

    print anObject.testVariableA # "This is another test. In fact, this is the last one."

    print anObject.testVariableB # "Caffeine fuels the world."

    print anObject.testMethodA() # True

    print anObject.testMethodB() # False

     

    Conclusion

     

    As you've witnessed, Python itself is unbelievably object-oriented, and it supports both simple and complex object-oriented design. While scripting in Python, you will come across object orientation regularly. When you create a string, you are creating an object. When you work with files, you are working with objects as well. When you work with Python's extensive standard library, you are, again, working with objects. Objects make Python an incredibly easy language to work with.

     

    You now understand the basics of objects and object-oriented programming. You may now utilize your knowledge in your scripts, potentially simplifying the development process.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · I thought this was a well written article - thanks for the contribution.
       · This is a great article for someone with no object oriented experience. I suggest...
       · Thanks for the input! :)Anyone else?
       · PeytonWhy dont you write a series on this topic for us. Id love to see it....
       · This was by far one of the easiest to follow tutotials on object oriented...
       · I've been reding all your recent articles. It's good work - Thanks for...
       · Thanks!
       · Although this article is quite interesting, it may bring confusion between class and...
       · The examples are irritating rather than funny. Fix those (by making them concrete)...
       · This program don't work.>>> aa =SomeClass()>>> aa.SomeNumber()Traceback...
       · <a href="{link1}">{text1}</a>, <a href="{link2}">{text2}</a>
       · <a href="{link1}">{text1}</a>, <a href="{link2}">{text2}</a>
     

       

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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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