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

       

    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