Python
  Home arrow Python arrow Page 2 - Metaclasses: Blueprints of Blueprints
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
SunQuest
 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
     
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Metaclasses: Blueprints of Blueprints - The Barebones


    (Page 2 of 5 )

    Metaclasses are created just like classes, except they are a subclass of type or another metaclass. Actually, type is itself a metaclass. There isn't any interesting syntax involved with their creation:

    >>> class MetaClass ( type ):
     pass

    Now, to demonstrate how we can use our overly-primitive metaclass, let's create a class from it. There are two ways to do this. The first way is to create a class from our metaclass just as we would create an object from a normal class. However, we have to make a few changes, or we'll end up with this:

    >>> A = MetaClass()

    Traceback (most recent call last):
      File "<pyshell#40>", line 1, in -toplevel-
        A = MetaClass()
    TypeError: type() takes 1 or 3 arguments

    Unless you instruct them to do otherwise, metaclasses accept three arguments: the name of the class to be created, a tuple of the base classes involved and a dictionary containing the class's attributes:

    >>> A = MetaClass ( 'A', (), {} )
    >>> A
    <class '__main__.A'>

    We can see our metaclass of our class like this:

    >>> A.__class__
    <class '__main__.MetaClass'>

    Similarly, we can check the metaclass of an object like so:

    >>> z = A()
    >>> z.__class__.__class__
    <class '__main__.MetaClass'>

    Creating some attributes is not very complicated. Let's create a class with a few variables:

    >>> B = MetaClass ( 'B', (), { 'p': 4, 'q': ( '1', 2, '3' ), 'r': "%" } )
    >>> B.p
    4
    >>> B.q
    ('1', 2, '3')
    >>> B.r
    '%'

    Of course, this is pretty annoying and pointless for anything complex, which leads us to the second method of creating classes from metaclasses. You can simply assign the metaclass to a new-style class's __metaclass__ variable:

    >>> class C ( object ):
     __metaclass__ = MetaClass

    This is a more practical method of creating classes from metaclasses than the previous method.

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

    IBM developerWorks




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