Python
  Home arrow Python arrow Page 3 - 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 - Classes in Python
    ( Page 3 of 4 )

     

    Now that you have an understanding of what object orientation is, we'll move on to Python. The first logical step is to create a class. You can define a class pretty much anywhere, including inside conditional statements. Defining a class is simple:

     

    class SomeClass:

     

    The next step is to add a bit of data to the class. There isn't anything special or unique about this step:

     

    class SomeClass:

      "Just some class...that's all."

      someNumber = 127 + 1 / 360 * 2 + 1 / 2

      someString = 'Star Office 7'

     

    You can also add methods. These are defined just like functions, except they take self as their first argument:

     

    class SomeClass

      "Just some class...that's all."

      someNumber – 127 + 1 / 360 * 2 + 1 / 2

      someString = 'Star Office 7'

      def someFunction ( self ):

         return someString + ' tastes like chicken.'

      def someOtherFunction ( self ):

         return False

     

    As you can see, it's pretty simple to create a class with data and methods.

     

    Next, we must create an instance of our class, or an object derived from it. This is extremely simple and brief:

     

    someObject = SomeClass()

     

    We can now access the variables defined in our class:

     

    someObject.someNumber

    someObject.someString

     

    Calling the methods defined in our class is also very simple:

     

    someObject.someFunction()

    someObject.someOtherFunction()

     

    It is also possible to define attributes after we have created the object:

     

    someObject.someRandomVariable = 5

     

    Similarly, it is possible to clean up what we create:

     

    del someObject.someRandomVariable

     

    Let's say we want to define a variable when we first create an instance of our class. This can be accomplished by creating a special method of our class, __init__. The method will be executed when our class is first created.

     

    class AnotherClass:

      def __init__ ( self, anotherArgument ):

          self.anotherArgument = anotherArgument

     

    anotherObject = AnotherClass ( 'Hello object-oriented world.' )

    print anotherObject.anotherArgument # "Hello object-oriented world."

     



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