Home arrow Python arrow Page 4 - Object-Oriented Programming With Python (part 2)

Under The Microscope - Python

With the basics out of the way, this concluding article discussesmore advanced aspects of Python's OO implementation, including inheritance,destructors and overrides.

TABLE OF CONTENTS:
  1. Object-Oriented Programming With Python (part 2)
  2. The Family Tree
  3. Alarm Bells
  4. Under The Microscope
  5. Chaos And Destruction
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 13
November 26, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: