Python
  Home arrow Python arrow Page 4 - Object Orientation in Python
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Object Orientation in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 38
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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.



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek