Python
  Home arrow Python arrow Page 3 - More 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

More Object Orientation in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2005-12-06


    Table of Contents:
  • More Object Orientation in Python
  • Types and Classes
  • Properties
  • Methods: Instance, Class and Static

  • 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


    More Object Orientation in Python - Properties
    ( Page 3 of 4 )

    When we subclass a built-in type, the result is a new-style class that can take advantage of the new class features introduced in Python 2.2. However, we will obviously want to define our own classes and take advantage of the added features. This can be done by simply subclassing object:

    >>> class NewStyle ( object ):
     pass

    >>> type ( NewStyle )
    <type 'type'>

    With that done, let's take a look at some of the new features that can be used by new-style classes, starting with properties. Properties are used in a way that is similar to the way regular attributes are used. However, there is one special difference between properties and regular attributes. Take a look at this class, which represents a hurricane:

    >>> class Hurricane ( object ):
     def __init__ ( self, name = 'Unnamed', category = 1 ):
      self.name = name
      self.category = category

    We can either pass the name and category when creating the Hurricane instance, or we can later set them with an attribute:

    >>> hugo = Hurricane ( 'Hugo', 4 )
    >>> hugo.name
    'Hugo'
    >>> hugo.category
    4
    >>> ophelia = Hurricane()
    >>> ophelia.name = 'Ophelia'
    >>> ophelia.name
    'Ophelia'
    >>> ophelia.category
    1

    So far, everything is fine. We have Hurricane Hugo at Category Four, and we have Hurricane Ophelia at Category One. However, consider the following example:

    >>> andrew = Hurricane()
    >>> andrew.name = 'Andrew'
    >>> andrew.category = 6

    Here, we have a bit of a problem. Although Hurricane Andrew caused billions of dollars worth of damage, there is no “Category Six” for hurricanes. We're left with fixing this problem in our class. Sure, we could always require the user to pass the category when creating a Hurricane instance, but what's to stop them from changing it to whatever number they would like later on?

    >>> charley = Hurricane ( 'Charlie', 4 )
    >>> charley.category
    4
    >>> charley.category = 7
    >>> charley.category
    7

    There is a simple solution to our problem: properties. Properties allow us to control attributes, which is what we need to do to solve our problem. To create a property, we need to define two methods. One method will be responsible for obtaining the value of the property, and the other will be responsible for setting the value of the property. These method should manipulate an internal value, since the value and the property are two different things and obviously cannot share the same name. Here's our modified Hurricane class:

    >>> class Hurricane ( object ):
     
     __category = 1
     
     def getCategory ( self ):
      return self.__category 
     def setCategory ( self, category ):
      if category > 5:
       category = 5
      elif category < 1:
       category = 1
      self.__category = category
      
     category = property ( getCategory, setCategory )
     
     def __init__ ( self, name = 'Unnamed', category = 1 ):
      self.name = name
      self.category = category

    Now let's try to set unrealistic category values:

    >>> camille = Hurricane ( 'Camille', 8 )
    >>> camille.category
    5
    >>> danny = Hurricane()
    >>> danny.name = 'Danny'
    >>> danny.category = 0
    >>> danny.category
    1

    If we wanted to, we could make the name of the hurricane an attribute, too. We could prefix the name of the hurricane with “Hurricane” when the property's value was requested:

    >>> class Hurricane ( object ):
     
     __category = 1
     __name = "Unnamed"
     
     def getCategory ( self ):
      return self.__category 
     def setCategory ( self, category ):
      if category > 5:
       category = 5
      elif category < 1:
       category = 1
      self.__category = category
     def getName ( self ):
      return "Hurricane " + self.__name
     def setName ( self, name ):
      self.__name = name
      
     category = property ( getCategory, setCategory )
     name = property ( getName, setName )
     
     def __init__ ( self, name = 'Unnamed', category = 1 ):
      self.name = name
      self.category = category

    Here's our latest class in action:

    >>> georges = Hurricane ( 'Georges', 4 )
    >>> georges.name
    'Hurricane Georges'

    Properties are powerful tools, and they add a new level of intelligence to classes, allowing them to format outgoing data and change unrealistic incoming data. Again, however, properties can only be used in new-style classes.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek