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  
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 
Moblin 
JMSL Numerical Library 
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

More Object Orientation in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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


       · Hey, all.A few people asked me to create a follow-up to Object Orientation in...
       · Hi all,I have been asked to write a programm in python (no experience though) and...
     

       

    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 5 hosted by Hostway