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

Metaclasses: Blueprints of Blueprints
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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 5 hosted by Hostway
    Stay green...Green IT