Python
  Home arrow Python arrow Page 4 - Object-Oriented Programming With Pytho...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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-Oriented Programming With Python (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2001-11-26

    Table of Contents:
  • Object-Oriented Programming With Python (part 2)
  • The Family Tree
  • Alarm Bells
  • Under The Microscope
  • Chaos And Destruction

  • 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


    Object-Oriented Programming With Python (part 2) - Under The Microscope


    (Page 4 of 5 )

    A number of built-in functions are available to help you navigate Python's classes and objects.

    The most basic task involves distinguishing between classes and instances - and the type() function can help here. Take a look:

    >>> type(veryBigSnake) <type 'class'> >>> beta = veryBigSnake("Vanessa Viper", "viper") New snake in da house! >>> type(beta) <type 'instance'> >>>
    You may already be familiar with the dir() function, which returns a list of object properties and methods - look what it says when I run it on a class

    >>> dir(veryBigSnake) ['__del__', '__doc__', '__init__', '__module__', 'set_snake_name', 'set_snake_type', 'who_am_i'] >>>
    and on an object of that class.

    >>> dir(beta) ['name', 'type'] >>>
    Every class also exposes the __bases__ property, which holds the name(s) of the class(es) from which this particular class has been derived. Most of the time, this property does not contain a value; it's only useful if you're working with classes which inherit methods and properties from each other.

    >>> # base class - has no ancestors >>> veryBigSnake.__bases__ () >>> # derived class - has base class >>> evenBiggerSnake.__bases__ (<class snake.veryBigSnake at 80d5c08>,) >>>
    If you'd like to see the values of a specific instance's properties, you can use the instance's __dict__ property, which returns a dictionary of name-value pairs,

    >>> beta.__dict__ {'name': 'Vanessa Viper', 'type': 'viper'} >>>
    while the corresponding __class__ property identifies the class from which this instance was spawned.

    >>> beta.__class__ <class snake.veryBigSnake at 80cda20> >>>

    More Python Articles
    More By icarus, (c) Melonfire


     

       

    PYTHON ARTICLES

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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT