Python
  Home arrow Python arrow Page 4 - 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 - Methods: Instance, Class and Static
    ( Page 4 of 4 )

    Let's take a simple class and build a simple object from it:

    >>> class SimpleClass:
     def simpleMethod ( self ):
      return True


    >>> simpleObject = SimpleClass()

    So, basically, we have the class SimpleClass and an instance of that class, simpleObject. Let's say we want to call the method simpleMethod. We cannot do so by calling the method in the context of the class, or we get an error:

    >>> SimpleClass.simpleMethod()

    Traceback (most recent call last):
      File "<pyshell#5>", line 1, in -toplevel-
        SimpleClass.simpleMethod()
    TypeError: unbound method simpleMethod() must be called with SimpleClass instance as first argument (got nothing instead)

    This is because simpleMethod is an instance method. Each instance of SimpleClass has the method, and the method of one instance is called independently. On the other hand, we have static methods and class methods, which do not have relationships with the instances of their class. Let's take a look at static methods first. Declaring one is very straightforward:

    >>> class StaticTest:
     def staticExample ( number ):
      return number ** .5
     staticExample = staticmethod ( staticExample )

    Before we continue, notice that what we have created is not a new-style class. Static methods and classes work with old-style classes and new-style classes.

    Now, try calling the staticExample method:

    >>> StaticTest.staticExample ( 25 )
    5.0

    As you can see above, the method we created can be called within the context of our class. No instance is involved anywhere in the process, and the self variable is not present. Neither is the cls variable, however, so we are not able to do anything special to the class. This shortfall can be addressed by using class methods, which are equally as easy to create. Let's let a class, PythonBurgers, represent a chain of restaurants. For the chain of restaurants, we want to keep track of how many hamburgers have been sold. With class methods, we can do this in a nice, object-oriented way:

    >>> class PythonBurgers:
     hamburgers = 0
     def sellHamburgers ( cls, amount = 1 ):
      cls.hamburgers = cls.hamburgers + amount
     sellHamburgers = classmethod ( sellHamburgers )

    Now we're ready to sell ourselves the first hamburger of our business. We can call the method sellHamburgers in the context of the PythonBurgers class:

    >>> PythonBurgers.sellHamburgers ( 2 )
    >>> PythonBurgers.hamburgers
    2

    As joint owners of a chain of restaurants, we've consumed two hamburgers. However, since we want a chain of restaurants, it makes sense to offer entrepreneurs the chance to open franchises. Each franchise will be a little different, but they will share the same parent:

    >>> franchise1 = PythonBurgers()
    >>> franchise2 = PythonBurgers()
    >>> franchise3 = PythonBurgers()

    Now let's have each franchise sell some hamburgers:

    >>> franchise1.sellHamburgers ( 11 )
    >>> franchise2.sellHamburgers ( 48 )
    >>> franchise3.sellHamburgers ( 35 )

    The result is as follows:

    >>> PythonBurgers.hamburgers
    96


    Since our class method belongs to a class instead of its instances, it affects the class and not its instances. This behavior is pretty interesting.

    Conclusion

    While you may have noticed a gap between types and classes in earlier versions of Python, Python 2.2 has managed to bridge the gap, allowing built-in types to be subclassed and extended in ways that were not previously possible. Python 2.2 has also brought other changes to object orientation in Python, with one of the changes being properties. Properties allow the way in which attributes are set and retrieved to be easily governed. This can insure that the values coming into an object make sense, and it allows the object to format values retrieved.

    The other change we have covered is static methods and instances. Instead of being specific to a particular instance, static and class methods are related to the class in which they are defined. This allows new tasks to be completed in an object oriented way.



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