Python
  Home arrow Python arrow Page 3 - Object Orientation in Python
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


       · I thought this was a well written article - thanks for the contribution.
       · This is a great article for someone with no object oriented experience. I suggest...
       · Thanks for the input! :)Anyone else?
       · PeytonWhy dont you write a series on this topic for us. Id love to see it....
       · This was by far one of the easiest to follow tutotials on object oriented...
       · I've been reding all your recent articles. It's good work - Thanks for...
       · Thanks!
       · Although this article is quite interesting, it may bring confusion between class and...
       · The examples are irritating rather than funny. Fix those (by making them concrete)...
       · This program don't work.>>> aa =SomeClass()>>> aa.SomeNumber()Traceback...
       · <a href="{link1}">{text1}</a>, <a href="{link2}">{text2}</a>
       · <a href="{link1}">{text1}</a>, <a href="{link2}">{text2}</a>
     

       

    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...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway