Python
  Home arrow Python arrow Metaclasses: Blueprints of Blueprints
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

Metaclasses: Blueprints of Blueprints
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-12-13

    Table of Contents:
  • Metaclasses: Blueprints of Blueprints
  • The Barebones
  • Adding Some Meat
  • Using Metaclasses
  • A class named G

  • 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
     
     
    FaxWave - Free Trial.
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Metaclasses: Blueprints of Blueprints
    (Page 1 of 5 )

    We all know that objects are created from classes. But can a class be created from a class? Yes it can; this is called a metaclass. They give us a great deal of power when it comes to changing the behavior of a class. Python 2.2 supports metaclasses. Peyton McCullough explains.

    Prerequisites

    This article assumes that you are familiar with the new-style classes introduced in Python 2.2. If you are not familiar with these new classes, the article “More Object Orientation” is worth taking a look at. Likewise, if you are completely unfamiliar with object orientation in Python, the article “Object Orientation in Python” will familiarize you with the concept.

    Blueprints of Blueprints

    We are all familiar with the relationship between classes and objects: an object is created from a class. In this model, and I'm sure you have heard this analogy many times before, the class serves as a blueprint for the object. While this two-component model is fine for the majority of situations where object orientation is used, there is another component that you may or may not have heard of before. Let's put that aside for just a second, though.

    The creation of objects from classes is a fairly customizable process. Using a class, we can tailor an object to make it unique – to suit a specific purpose. For example, take a look at this class:

    >>> class Style:
     def __init__ ( self, foreground, background, font, size ):
      self.foreground = foreground
      self.background = background
      self.font = font
      self.size = size

    From the Style class, we can easily create numerous instances, each with its own unique attributes:

    >>> plain = Style ( 'black', 'white', 'Verdana', 9 )
    >>> ugly = Style ( 'purple', 'green', 'Arial', 18 )
    >>> inverse = Style ( 'white', 'black', 'Verdana', 9 )
    >>> christmas = Style ( 'green', 'red', 'Arial', 1 )
    >>> halloween = Style ( 'orange', 'black', 'Arial', 10 )
    >>> msOffice = Style ( 'black', 'white', 'Times New Roman', 12 )
    >>> starOffice = Style ( 'black', 'white', 'Thorndale', 12 )

    Each object is created exactly how we want it. We can also make this process dynamic, generating the objects without directly creating them from within the code:

    >>> import random
    >>> colors = [ 'black', 'white', 'green', 'red', 'orange', 'brown', 'yellow' ]
    >>> fonts = [ 'Verdana', 'Arial', 'Times New Roman', 'Helvetica' ]
    >>> styles = []
    >>> for x in xrange ( 10 ):
     styles.append ( Style ( random.choice ( colors ), random.choice ( colors ), random.choice ( fonts ), random.randint ( 7, 20 ) ) )

    Now let's reintroduce that third component I was talking about earlier. If objects can be created uniquely and dynamically, and if classes are technically objects in Python, then can classes be created the same way? Does Python support blueprints of blueprints? The answer is yes. Python supports classes of classes. These are called metaclasses, and their instances are classes themselves.

    More Python Articles
    More By Peyton McCullough


       · This is the metaclasses article that Peyton McCullough mentioned last week. I hope...
     

       

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




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